我刚刚开始使用 Webpack,当我尝试使用 jQuery 时遇到问题。我是 Webpack 的新手,所以如果我的问题看起来很基本,请道歉。
这是我的 webpack.config.js:
var webpack = require("webpack");
var path = require("path");
var bower_dir = __dirname + "/public/js/bower_components";
var compiled_dir = __dirname + "/public/js/compiled";
module.exports = {
entry: {
signup: compiled_dir + "/signup"
},
plugins: [
new webpack.ProvidePlugin({
$ : "jquery",
jQuery : "jquery",
"window.jQuery" : "jquery",
"root.jQuery" : "jquery"
})
],
resolve: {
alias: {
jquery : bower_dir + "/jquery/src/jquery",
}
},
output: {
path: path.join(__dirname, "public/js/dist"),
filename: "[name].bundle.js"
}
};
由于我使用的是 ProvidePlugin(用于 jQuery 插件),因此我的文件开头没有 require("jquery") 。 但是,在执行时,我在控制台上得到以下输出:
TypeError: jQuery.expr is undefined http://localhost:9000/js/dist/signup.bundle.js Line 6250
你有什么线索吗?
提前致谢。
最佳答案
通过删除 webpack.config.js 中的“resolve”部分并使用 npm 安装 jquery 来解决。 但是,如果有人可以向我解释为什么它现在有效,我将非常感激:-)
关于javascript - 将 webpack 与 jQuery 结合使用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29858162/