gpt4 book ai didi

javascript - 如何在与 webpack 捆绑的文件中动态导入对等依赖项?

转载 作者:行者123 更新时间:2023-11-30 19:57:13 25 4
gpt4 key购买 nike

我正在开发一个使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com