Issues running JavaScript tests in VS Code

Recently I was involved in troubleshooting an interesting issue with running unit tests for a Javascript project that was depending on a native Node module. The key step in resolving the issue was when we determined that the tests ran fine from the command line and only failed when the Mocha Test Explorer extension for Visual Studio Code was used.

The tests failed with the following error:

Error: The module '\\?\c:\Git\project\node_modules\gl\build\Release\webgl.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 72. This version of Node.js requires
NODE_MODULE_VERSION 75. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at process.func (electron/js2c/asar.js:140:31)
    at process.func [as dlopen] (electron/js2c/asar.js:140:31)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:969:18)
    at Object.func (electron/js2c/asar.js:140:31)
    at Object.func [as .node] (electron/js2c/asar.js:140:31)
    at Module.load (internal/modules/cjs/loader.js:782:32)
    at Module._load (internal/modules/cjs/loader.js:695:12)
    at Module._load (electron/js2c/asar.js:717:26)
    at Function.Module._load (electron/js2c/asar.js:717:26)
    at Module.require (internal/modules/cjs/loader.js:822:19)

This wasn't the first time I've encountered errors of this type. They can only occur with native Node modules and in my previous experience, they were always caused by one of the following:

  • The Node.js version on the local machine was recently upgraded (or downgraded) and the Node modules weren't reinstalled afterward. The installed native Node modules targeted the Node version from when they were installed. Since the Node version changed in the meantime, they were now targeting the wrong version. Deleting the node_modules folder and reinstalling the Node modules using npm i should resolve this problem.

  • There's no prebuilt version of the native Node module for the installed version of Node. This usually happens when trying to run an old version of the native Node module from before the Node version currently in use was released. This can usually be resolved by updating the Node module to a more recent version. If that's not an option, the native Node module can be compiled locally. If all prerequisites for the node-gyp build tool are correctly installed, the build should trigger automatically when installing the module.

Neither of the two options was true in this case. And to make matters even weirder, there wasn't any Node version released that would require NODE_MODULE_VERSION 75 as mentioned in the error message.

However, there is a commit in the Node repository mentioning this version concerning Electron. With Visual Studio Code being an Electron application, things now started to make sense. Electron uses different Node versions from the ones that are released standalone. For some reason, the tests were running using the Node engine built into Visual Studio Code. The presence of the electron folder in the call stack of the error above further confirmed this suspicion.

So, why was the Mocha Test Explorer running the tests using the Visual Studio Code Node engine? The answer lies in its documentation for the mochaExplorer.nodePath configuration option:

The path to the node executable to use. By default it will attempt to find it on your PATH, if it can't find it or if this option is set to null, it will use the one shipped with VS Code.

That's what was happening. Hardcoding the path to the Node executable in this configuration option immediately resolved the issue. The longterm solution was to fix the local Node installation so that the Node executable folder was included in PATH. I can imagine that restarting Visual Studio Code could also fix the issue if any changes were made to the local Node installation while Visual Studio Code was running.

If you want to reproduce this error yourself, you can download a sample project code from my GitHub repository. In it, I misconfigured Mocha Test Explorer to cause this error. Of course, you also need to have the extension installed and use it to run the sample test included in the project.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Creative Commons License