53

I'm new to Vue and webpack in general and I'm having a hard time figuring out how to import things.

I created a fresh Vue project through vue init I added bootstrap 4 with yarn add [email protected]

In main.js I try to import bootstrap and jquery:

import Vue from 'vue';
import jQuery from 'jquery';
import bootstrap from 'bootstrap';
import App from './App';
import router from './router';

But I get:

Uncaught Error: Bootstrap's JavaScript requires jQuery. jQuery must be included before Bootstrap's JavaScript.

window.jQuery = window.$ = $; does not work

Finally, where and how do I load the Sass such that it's available to the whole app?

7

13 Answers 13

54

I was having the same issue and this is what I ended up with however, I didn't use Bootstrap alpha since the beta has since been released.


  1. Install Bootstrap along with its dependencies, jQuery and Popper.js:
npm install [email protected] popper.js jquery --save-dev
  1. Edit your /build/webpack.dev.conf.js file to add a plugin for jQuery and Popper.js to deal with dependencies (you can read more about that in the Bootstrap docs):
plugins: [
    // ...
    new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery',
        Popper: ['popper.js', 'default'],
        // In case you imported plugins individually, you must also require them here:
        Util: "exports-loader?Util!bootstrap/js/dist/util",
        Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown",
        // ...
    })
    // ...
]
  1. Edit /src/main.js to import Bootstrap into the app's entry point:
import Vue from 'vue'
import App from './App'
import router from './router'
import 'bootstrap' // ←
  1. Edit the App.vue' file's <style> portion to import Bootsrap into your app's root css:
<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'app'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

@import'~bootstrap/dist/css/bootstrap.css'
</style>
  1. Run your Vue app from the CLI
npm start

Screenshot of the Vue.js project


I had to add the @import rule after the #app selector, otherwise the scoped styling would be canceled out by the Bootstrap import. I think there is a better way to do this so I will update my answer once I figure it out.

Sign up to request clarification or add additional context in comments.

2 Comments

Looks like you can, instead of importing into the css, in your main.js file just below import 'bootstrap' you can add import '../node_modules/bootstrap/dist/css/bootstrap.css';
I think that these should be installed as regular dependencies not -dev dependencies
49

You don't want to do any kind of adjustment in webpack.config.js like adding jQuery global in plugin array. Once you installed all bootstrap dependencies like jQuery and popper than just import them in app.vue or main.js (you can import separately in each component if you wish)

import 'bootstrap/dist/css/bootstrap.min.css'
import 'jquery/src/jquery.js'
import 'bootstrap/dist/js/bootstrap.min.js'

thats it ..

2 Comments

This should become the selected answer
This is it. What is that webpack haystack as accepted answer?
18

I will try to be as precise as possible

1.Go the the Vue Cli project run :

npm i bootstrap jquery popper.js

2.Go to src/main.js import like:

import 'bootstrap'; import 'bootstrap/dist/css/bootstrap.min.css';

Comments

11

Only For Bootstrap 5

Follow the below in cmd

  1. vue create project-name

  2. Select Vue version

  3. cd project-name

  4. npm i bootstrap @popperjs/core (just bootstrap means latest available bootstrap version)

                  (or)
    

    i)npm i bootstrap (to install bootstrap)

    ii)npm install --save @popperjs/core (to install popper.js)

    Note: Try this if only point 4 emits error

  5. Add the below to src/main.js import 'bootstrap'; import 'bootstrap/dist/css/bootstrap.min.css';

  6. Done now bootstrap will work on Vue-Cli

Comments

8

With Vue CLI v3.x/v4.x, you can also use the @techiediaries/vue-cli-plugin-bootstrap to quickly add the latest Bootstrap 4 version in your Vue project without manually adding any files or settings.

Head over to a new command-line interface, navigate to your project's folder and run the following command:

$ vue add @techiediaries/bootstrap

The plugin will take care of adding any configuration options and install the dependencies into your project so you can start using Bootstrap classes right away without spending time figuring out what settings or dependencies you need to add.

Comments

5

For Gridsome users, the solution is exactly the same!

yarn add jquery bootstrap popper.js
import 'jquery/src/jquery.js';
import 'popper.js/dist/popper.min.js';
import 'bootstrap/dist/js/bootstrap.min.js';

You can also import the css here like this import 'bootstrap/dist/css/bootstrap.min.css' however bootstrap provides a SCSS file which can be highly customisable, if you want this kind of customisation, import in the main.js file a new scss file

// Load core styling
import '~/assets/styles/core.scss';

and then import bootstrap like this

@import '~bootstrap/scss/bootstrap';
@import 'theme.scss';

The theme.scss follows this document https://getbootstrap.com/docs/4.0/getting-started/theming/

Comments

1

As usual in JS ecosystem the answers get out of date very quick.. this is what is working for my right now:

$ yarn add jquery bootstrap @popperjs/core
├─ @popperjs/[email protected]
├─ [email protected]
└─ [email protected]
// main.js
import '@popperjs/core/dist/umd/popper';
import 'bootstrap/dist/css/bootstrap.min.css'
import 'jquery/src/jquery.js'
import 'bootstrap/dist/js/bootstrap.min.js'

Comments

1

Brother, Greetings! Simple way to install and configure Bootstrap in your Vue project is that

  1. First install bootstrap using a command in the terminal of the IDE which is below:

    npm i bootstrap

  2. You need to configure this in your main.js file so that it is applied to the whole project. You need to import this from node_modules folder to use it in project.

    import "../node_modules/bootstrap/dist/css/bootstrap.css";

    import "../node_modules/bootstrap/dist/js/bootstrap.bundle";

Just paste these lines in your main.js file because this is the path where your required bootstrap files will be if not then check the path and edit this path.

If you want to test whether it has worked or not, go to the app component and create a button, provide it a bootstrap class and run the project to see whether it is working or not.

Thanks, take care :)

Screenshot:

enter image description here

Comments

0

Depending on what packages you already have installed (such as Vuetify), you may not be able to use boostrap 4 + vue 3. Check your versions to confirm before following the directions in the thread!

Comments

0

It is very simple.

Step 1: Install bootstrap

npm install bootstrap

Now inside the module, the bootstrap folder gets added which confirms that the installation is successful.

Bootsrap installtion confirmation pic

Step 2: Import bootstrap into main.js folder

import "../node_modules/bootstrap/dist/css/bootstrap.css";
import "../node_modules/bootstrap/dist/js/bootstrap.bundle";

You are done

Hey 🥳 now you can see that Bootstrap is added. And add some bootstrap colors for testing.

boostrap added output on side

Comments

0

Simple 2 step process:

First install bootstrap

npm install bootstrap

Then include this just above the last line in your App.vue file

@import'~bootstrap/dist/css/bootstrap.css'

Like this: Image showing where to insert the above line

Then use bootstrap classes as usual in your html code!

Comments

-2

use Bootstrap CDN and include it as a in your index.html

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

I never used Bootsrap with Vue.js, as I'm not a fan. But when I used Bulma's sass, first I did npm install for Bulma. Then I created a folder in src/assets/sass and inside it created a main.scss and inside it I imported bulma by @import '~bulma/bulma';

1 Comment

This doesn't allow for usage of any of the jQuery functionality. It should be installed the way specified in the accepted answer
-2

jQuery is not required in current version (Right now: "bootstrap": "^4.4.1", "bootstrap-vue": "^2.5.0")

  • install bootstrap
    • npm install bootstrap-vue bootstrap --save
  • Then, register BootstrapVue in your app entry point (main.js)
    • import BootstrapVue from 'bootstrap-vue/dist/bootstrap-vue.esm';
    • Vue.use(BootstrapVue)
  • And import Bootstrap and BootstrapVue css files in main.js:
    • import 'bootstrap/dist/css/bootstrap.css'
    • import 'bootstrap-vue/dist/bootstrap-vue.css'

1 Comment

jQuery is required in bootstrap 4. However, the current bootstrap version 5.0.0.beta3 and future versions will not require jQuery. Your first sentence gave a false impression that jQuery is not a dependency for bootstrap 4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.