Error using Bootstrap & jQuery Packages in ES6 with Browserify

后端 未结 4 1954
滥情空心
滥情空心 2021-02-07 18:16

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          


        
相关标签:
4条回答
  • 2021-02-07 18:30

    @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 讨论(0)
  • 2021-02-07 18:37

    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');
    
    0 讨论(0)
  • 2021-02-07 18:47

    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.

    0 讨论(0)
  • 2021-02-07 18:52

    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

    0 讨论(0)
提交回复
热议问题