Webpack:使用另一个依赖项而不是它自己的子依赖项来建立依赖项

Webpack: Make dependency using another dependency instead of its own subdependency

我正在使用 webpack to build my app and I use a library (eventemitter4) that itself depends on another library (underscore).

但是,我已经在我的应用程序中使用 underscore (lodash 的替代品,更准确地说是 es6 版本)。 我希望 eventemitter4 使用这个后来的库并避免在我的构建中包含这两个库。

我使用 npm install 安装依赖项。结果,underscore 被捆绑为 eventemitter4 目录中的一个子目录。

它试图设置别名,但我无法让它工作:

alias: {
  "underscore": "lodash-es",
  "lodash": "lodash-es",
  "~/underscore": "lodash-es"
}

加注

ERROR in ./~/eventemitter4/index.js
Module not found: Error: Cannot resolve module 'lodash-es' in /Users/me/myapp/node_modules/eventemitter4
@ ./~/eventemitter4/index.js 6:2-23

如果我不加"~/underscore": "lodash-es",分underscore is included instead of lodash.

有什么想法吗?

非常感谢您的帮助。

其实我找到原因了。别名正在工作。错误原因是lodash-es在他的package.json中没有提供main属性(只提供了一个webpack无法识别的esnext:main

我使用以下方法解决了这个问题:

alias: {
  "underscore": "lodash-es",
  "lodash": "lodash-es/lodash",
  "~/underscore": "lodash-es"
}