使用Webpack时找不到Vertx模块

3
使用webpack和想要使用when.js(when)的库时,编译时可能会抛出以下错误:
[2] ERROR in ./node_modules/when/lib/env.js 32:14-35
[2] Module not found: Error: Can't resolve 'vertx' in 'path-to-project/node_modules/when/lib'

问题似乎只局限于webpack,并在whengithub上有记录。运行npm install vertxnpm install @vertx/core也无法解决问题,因为问题出现在when库中导入vertx时。
2个回答

0
问题确实可以通过添加@vertex/core包来解决,但是不建议像@Todd Rylaarsdam建议的那样手动编辑./node_modules/when/lib/env.js文件,而是应该在Webpack配置中使用NormalModuleReplacementPlugin来替换旧的vertx包,代码如下:
new webpack.NormalModuleReplacementPlugin(
  /^vertx/,
  '@vertx/core',
)

这样,您就不必手动更改您正在使用的软件包的文件。

有关此插件的更多信息,请参见https://webpack.js.org/plugins/normal-module-replacement-plugin/


0
为了解决这个问题:
安装@vertx/corenpm i @vertx/core
编辑文件./node_modules/when/lib/env.js,并将第32行更改为
var vertx = vertxRequire('vertx');

var vertx = vertxRequire('@vertx/core');

这将引用@vertx/core包,而不是vertx包。当使用webpack和npm时,似乎找不到vertx包。我只使用vertx/core没有遇到任何问题。如果其他人也在您的项目上工作,您可能还需要更新gitignore以包括对库的更改。


我想补充一点,这应该通过gulpfile和/或一些自动机制来完成,因为每次执行新的安装时,node_modules都会被覆盖。 - Jose A

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接