13

I'm trying to use Bootstrap with jQuery. I'm using Browserify with a Babel transform to compile. I get the following error.

Uncaught ReferenceError: jQuery is not defined

I've tried importing the packages like this, but I get the above error.

import $ from 'jquery';
import Bootstrap from 'bootstrap';

Searching around, I found this answer, so I tried this but I still get the same error.

import $ from 'jquery';
window.$ = window.jQuery = $;
import Bootstrap from 'bootstrap';
Bootstrap.$ = $;

Am I importing these packages incorrectly with ES6 or is it done in a different way? The last thing I tried was importing the packages without ES6 like this, but I still got the same error.

window.$ = window.jQuery = require('jquery');
var Bootstrap = require('bootstrap');
Bootstrap.$ = $
12
  • Why are you trying to import the files like this, any specific use case? Commented Dec 6, 2015 at 17:17
  • 1
    I'm importing them because that's how browserify works. You import packages like this and then compile them into one js file. Commented Dec 6, 2015 at 17:19
  • Are they located in the same folder as the .js file you're importing them in? Commented Dec 6, 2015 at 17:20
  • 1
    @NickKenens All my packages are inside a node_modules directory. I have no problem with any of the packages that are inside the node_modules. The problem only occurs when I try to use the bootstrap package. Commented Dec 6, 2015 at 17:23
  • 1
    This is getting fixed for BS4 hopefully soon, see github.com/twbs/bootstrap/issues/17201 Commented Dec 6, 2015 at 17:24

4 Answers 4

10

The accepted answer helped me to right track but the final solution for me was a bit shorter so I will post also here.

// Importing jQuery in ES6 style
import $ from "jquery";

// We need to expose jQuery as global variable
window.jQuery = window.$ = $;

// ES6 import does not work it throws error: Missing jQuery 
// using Node.js style import works without problems
require('bootstrap');
Sign up to request clarification or add additional context in comments.

1 Comment

I like this solution totally instead of writing plugin section in webpack configuration.
3

As mentioned by Pete TNT, there is a bug in the Bootstrap package I am using. I switched to using this Bootstrap package and made the following changes to my code.

window.$ = window.jQuery = require('jquery');
var Bootstrap = require('bootstrap-sass');
Bootstrap.$ = $;
require('../../../../../node_modules/bootstrap-sass/assets/javascripts/bootstrap');

It looks like, for the time-being, ES6 imports will not work with jquery and bootstrap packages in the way I was trying to use them.

3 Comments

FYI, I got it working with the original bootstrap package just by using require() instead of import.
@Enijar "It looks like, for the time-being, ES6 imports will not work with jquery and bootstrap packages" Is this still the case in 2016, because I cannot also import bootstrap-saas and its dependency jquery using ES6 modules syntax
this worked for me. is there a more clean way of doing it?
2

I've tried many many solutions but for some reason I had to define the global jquery in lowercase on the base script (eg. main.js)

import jQuery from 'jquery'
global.jQuery = jQuery
global.jquery = jQuery // jquery lowercase was the solution for me
global.$ = jQuery
let Bootstrap = require('bootstrap')
import 'bootstrap/dist/css/bootstrap.css'

This solutions also works for vue-cli + jquery + bootstrap

Comments

0

@Enijar's solution didn't work for me. Had to use old CDN method.

<script   src="https://code.jquery.com/jquery-3.1.0.min.js"   
integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="   
crossorigin="anonymous"></script>

https://code.jquery.com/

Comments

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.