为什么webpack生成的JS包抱怨缺少jQuery?

时间:2017-06-10 16:43:34

标签: javascript jquery webpack webpack-2

我在我的脚本中使用了jQuery,并在我的require文件中添加了entry(在webpack.config.js中定义)。我尝试了这些导入中的每一个:

import 'jquery'
var $ = require("jquery")
import $ from 'jquery'

汇编表明它确实包括在内:

D:\Dropbox\dev\jekyll\blog>webpack
Hash: 3a4538db28bec4591dd8
Version: webpack 2.6.1
Time: 3088ms
   Asset    Size  Chunks                    Chunk Names
build.js  562 kB       0  [emitted]  [big]  main
   [0] ./webpack/js/explorer.js 1.37 kB {0} [built]
   [1] ./~/bootstrap/dist/js/npm.js 484 bytes {0} [built]
   [2] ./~/jquery/dist/jquery.js 268 kB {0} [built]
(...)

在浏览器中运行我的脚本时,我在控制台上收到一条错误消息:

build.js:20018 Uncaught ReferenceError: jQuery is not defined
    at Object.<anonymous> (build.js:20018)
    at __webpack_require__ (build.js:20)
    at Object.<anonymous> (build.js:129)
    at __webpack_require__ (build.js:20)
    at Object.<anonymous> (build.js:17585)
    at __webpack_require__ (build.js:20)
    at Vue.el (build.js:66)
    at build.js:69
(anonymous) @ build.js:20018
__webpack_require__ @ build.js:20
(anonymous) @ build.js:129
__webpack_require__ @ build.js:20
(anonymous) @ build.js:17585
__webpack_require__ @ build.js:20
Vue.el @ build.js:66
(anonymous) @ build.js:69

这个错误是什么意思?

然后我偶然发现another question(没有答案)有类似问题,并尝试通过添加到webpack.config.js

随后修改我的文件
const webpack = require('webpack')

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

和简单的

import 'jquery' 

在我的输入文件中。

问题保持不变。

0 个答案:

没有答案