未找到 Webpack 模块:错误:无法解析“jquery" [英] Webpack Module not found: Error: Can't resolve 'jquery'
问题描述
当我运行 'webpack' 命令时,出现此错误:
When I run the 'webpack' command, I get this error:
./js/main.js 中的错误找不到模块:错误:无法解析 '...js' @ ./js/main.js 中的 'jquery' 3:0-16 4:0-23
ERROR in ./js/main.js Module not found: Error: Can't resolve 'jquery' in '...js' @ ./js/main.js 3:0-16 4:0-23
在 package.json 我有:
In package.json I have:
"devDependencies": {
"handlebars": "^4.0.6",
"handlebars-loader": "^1.4.0",
"jquery": "^3.2.1",
"path": "^0.12.7"
},
在 webpack.config.js 中:
In webpack.config.js:
var path = require("path");
module.exports = {
entry: "./js/main.js",
output: {
path: __dirname + "/js",
filename: "scripts-bundled.js"
},
resolve: {
modules: [
path.join(__dirname, "js/helpers")
]
},
module: {
loaders: [
{test: /.hbs$/, loader: "handlebars-loader"}
]
}
};
在文件顶部的 main.js 中,我有:
And in main.js at the top of the file, I have:
import $ from 'jquery';
我也在 main.js 中使用了把手.可能是handlebars 或handlebars-loader 干扰了jquery?我之前在另一个没有使用把手的项目中使用过 webpack 和 jquery 没有这个问题,但可能与它无关.
I'm also using handlebars in main.js. Could it be that handlebars or handlebars-loader is interfering with the jquery? I've used webpack and jquery without this issue before in another project where I didn't use handlebars, but maybe it has nothing to do with it.
推荐答案
车把与此无关.问题是您更改了 resolve.modules
[path.join(__dirname, "js/helpers")]
.所以 webpack 只会在 js/helpers
中查找任何模块,但是 jquery
和来自 npm 的其他依赖项在 node_modules
中.resolve.modules
的默认值为 ["node_modules"]
.您还需要添加 node_modules
以保持常规模块分辨率.
The handlebars has nothing to do with it. The problem is that you changed resolve.modules
to [path.join(__dirname, "js/helpers")]
. So webpack will only look in js/helpers
for any module, but jquery
and other dependencies from npm are in node_modules
. The default value of resolve.modules
is ["node_modules"]
. You also need to add node_modules
to keep the regular module resolution.
resolve: {
modules: [
path.join(__dirname, "js/helpers"),
"node_modules"
]
},
这篇关于未找到 Webpack 模块:错误:无法解析“jquery"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!