最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error using Bootstrap & jQuery Packages in ES6 with Browserify - Stack Overflow

programmeradmin1浏览0评论

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.$ = $

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.$ = $
Share Improve this question edited May 23, 2017 at 11:55 CommunityBot 11 silver badge asked Dec 6, 2015 at 17:12 James CraigJames Craig 6,85410 gold badges48 silver badges77 bronze badges 12
  • Why are you trying to import the files like this, any specific use case? – Nick Snick 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. – James Craig Commented Dec 6, 2015 at 17:19
  • Are they located in the same folder as the .js file you're importing them in? – Nick Snick 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. – James Craig Commented Dec 6, 2015 at 17:23
  • 1 This is getting fixed for BS4 hopefully soon, see github.com/twbs/bootstrap/issues/17201 – Pete TNT Commented Dec 6, 2015 at 17:24
 |  Show 7 more comments

4 Answers 4

Reset to default 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');

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.

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

@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/

发布评论

评论列表(0)

  1. 暂无评论