2021-01-09 09:05

(index):72 Uncaught ReferenceError: $ is not defined

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


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • weixin_39585761 weixin_39585761 3月前

    I was getting the same error using the eonasdan-bootstrap-datetimepicker widget.

    I tried several things, including adding the last two lines here to the ProvidePlugin section of webpack.config.js:

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

    The only thing that actually worked, however, was changing require('jquery'); in main.js to this:

    window.$ = window.jQuery = require('jquery');

    This might not be ideal, though. Does anyone have a better solution?

    点赞 评论 复制链接分享
  • weixin_39669701 weixin_39669701 3月前

    I was able to get this resolved by adding the expose-loader, and specifying this as the loader in the exports:

    npm i expose-loader -s

    And then in

    module: { 
      loaders { 
        { test: require.resolve('jquery'), loaders: ['expose-loader?$', 'expose-loader?jQuery'] },
    点赞 评论 复制链接分享
  • weixin_39585761 weixin_39585761 3月前

    This worked for me and seems like a much cleaner solution.

    Thanks, !

    点赞 评论 复制链接分享
  • weixin_39560245 weixin_39560245 3月前

    Closing, as this issue is stale. Feel free to reopen or comment if there's still an issue.

    点赞 评论 复制链接分享