vertx module not found when using webpack

Viewed 870

When using webpack and libraries that want to use when.js (when), it is possible that the following error is thrown when compiling:

[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'

The issue seems to be isolated to webpack, and is documented on when's github. Running npm install vertx or npm install @vertx/core will not resolve the issue either, since the problem lays in the import of vertx in the when library.

2 Answers

To resolve the issue:

Install @vertx/core (npm i @vertx/core)

Edit the file ./node_modules/when/lib/env.js, and change line 32 from

var vertx = vertxRequire('vertx');

to

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

This will reference the @vertx/core package instead of the vertx package, which does not seem to be able to be found when using webpack and npm. I haven't experienced any issues with just using vertx/core. You might also need to update your gitignore to include the changes to the library if others are also working on your project.

The issue can indeed be solved by adding the @vertex/core package, but instead of editing the ./node_modules/when/lib/env.js file manually as recommended by @Todd Rylaarsdam, I'd use the NormalModuleReplacementPlugin in your Webpack config to replace the old vertx package, like so:

new webpack.NormalModuleReplacementPlugin(
  /^vertx/,
  '@vertx/core',
)

This way you don't have to make any manual changes to the files of a package you're using.

See https://webpack.js.org/plugins/normal-module-replacement-plugin/ for more info about this plugin.

Related