我正在开发一个使用 jQuery 作为对等依赖项的插件。当我尝试将此插件导入到我的主项目(已安装 jQuery)时,出现错误 Module not found: Error: Can't resolve 'jquery' in <(plugin's folder)>
.似乎当我尝试动态导入 jQuery(对等依赖项)时,webpack 会查看插件的 node_modules
编译时的文件夹,而不是根项目的 node_modules
,即使它是对等依赖。如何让 webpack 查看根项目的 node_modules
而不是插件的 node_modules
?
Webpack.config.js:
var webpack = require('webpack');
module.exports = {
entry: './main.js',
output: {
filename: 'bundle.js'
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})
],
watch: true,
resolve: {
alias: { jquery: "jQuery" }
}
};
最佳答案
我想我通过将此添加到我的 webpack 配置的 resolve
选项找到了解决方案:
modules: [
path.resolve('./node_modules'),
path.resolve('../node_modules')
]
编辑:当我实际从 npm 导入项目后,问题似乎完全消失了,而在我使用 npm 链接之前。不再需要上述解决方案。
关于javascript - 如何在与 webpack 捆绑的文件中动态导入对等依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53841804/