'jquery is not defined' when using webpack to load bootstrap

后端 未结 4 1373
故里飘歌
故里飘歌 2020-12-14 06:34

I am just starting to learn to use Webpack (previously I just use the manual way to include individual scripts separately). And I used bootstrap-loader for load

相关标签:
4条回答
  • Plugin is not needed. Just use following as entry:

    entry: [
        'jquery', //will load jquery from node_modules
        './assets/js/index',
    ]
    

    Then in ES6+ file use:

    import $ from "jquery";
    
    0 讨论(0)
  • 2020-12-14 07:02

    I want to share my findings for any future developer who might have the same use-case I do.

    I had the requirement to only create a vendor bundle and not an app bundle for an AngularJS (1.7.2) app, using Webpack 4 (4.16.4). I think because I was only creating a vendor bundle, none of the other solutions worked. Only expose-loader worked for me, like so:

        // webpack.config.js
        module: {
            rules: [
                {
                    test: require.resolve('jquery'),
                    use: [{
                            loader: 'expose-loader',
                            options: 'jQuery'
                        },
                        {
                            loader: 'expose-loader',
                            options: '$'
                        }
                    ]
                }
            ]
        }
    

    For more information: https://github.com/webpack-contrib/expose-loader

    0 讨论(0)
  • 2020-12-14 07:05

    Add this as plugin

      new webpack.ProvidePlugin({
       $: "jquery",
       jQuery: "jquery"
      })
    

    and you should be able to have jquery in your whole project.

    If issue persists after adding the plugin, try restarting your nodejs server. Remember to install jquery using npm install --save jquery.

    0 讨论(0)
  • 2020-12-14 07:11

    I know this is a bit old, but I managed to do it like that :

    global.jQuery = require('jquery'); require('bootstrap');

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