The module was compiled against a different Node.js version using NODE_MODULE_VERSION

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
5 min

banner

# Table of Contents

  1. The module was compiled against a different Node.js version using NODE_MODULE_VERSION
  2. Try reinstalling the package
  3. Try to rebuild the package with the npm rebuild command
  4. Solving the error when using Electron
  5. Delete your node_modules and package-lock.json files
  6. Solve the error when using Docker
  7. Make sure the Node.js version in your terminal matches your IDE's version
  8. Install the long-term supported version of Node.js

# The module was compiled against a different Node.js version using NODE_MODULE_VERSION

The error "The module 'X' was compiled against a different Node.js version using NODE_MODULE_VERSION" occurs when a module was compiled using a different Node.js version than the one that is currently active.

To solve the error, remove the module from your node_modules directory and rerun npm install.

was compiled against different node js version using node module version

Here is the complete stack trace.

shell
Error: The module 'X' was compiled against a different Node.js version using NODE_MODULE_VERSION 111. This version of Node.js requires NODE_MODULE_VERSION 108. Please try recompiling or re-installing the module (for instance, using `npm rebuild` or `num install`).

Your error message will contain the name of the package that caused the issue.

The package was built using a different Node.js version than the currently active one.

Open your terminal in your project's root directory (where your package.json) file is and run the following command to delete the module from your node_modules directory.

The following command works on macOS and Linux.

shell
rm -rf node_modules

remove node modules folder

And the following command works on Windows.

shell
rd /s /q "node_modules"

Run the following command to clear your cache.

shell
npm cache clean --force

clean npm cache

After you remove the directory, rerun the npm install command to rebuild your dependencies using your current Node.js version.

shell
# with NPM npm install # or with YARN yarn install

install your modules

After you reinstall your modules with your current Node.js version, the issue should be resolved.

# Try reinstalling the package

If the error persists, try to reinstall the package.

The name of the package should be contained in your error message, e.g.:

  • Error: The module '/bobbyhadz/node_modules/bcrypt/lib/binding/bcrypt_lib.node' was compiled against a different Node.js version using NODE_MODULE_VERSION.
Note: The error commonly occurs when using the node-canvas package.

The error message above shows that the bcrypt package caused the issue.

You can reinstall the package by running the following commands.

Make sure to replace bcrypt with the name of the package from your error message.
shell
# with NPM npm uninstall bcrypt npm install bcrypt

If you use the yarn package manager, use the following commands instead.

shell
# or with YARN yarn remove bcrypt yarn add bcrypt

Make sure to replace bcrypt with the package from your error message and check if the error is resolved after reinstalling the package.

The error also commonly occurs when using the node-canvas package.

# Try to rebuild the package with the npm rebuild command

If the error persists, try to rebuild the package by running the following command.

shell
npm rebuild --update-binary

run npm rebuild command

You can also try to run the command without the --update-binary option.

shell
npm rebuild

If the error persists, try to run the command for the specific package.

Make sure to replace bcrypt with the name of the package from your error message.

shell
npm rebuild bcrypt --update-binary

# Solving the error when using Electron

If you got the error when using electron:

  1. Require all dependencies in the entry point of your application (e.g. your main.js file).
  2. Install the electron-rebuild package.
shell
npm install --save-dev @electron/rebuild
  1. Delete your node_modules directory.

If you are on Windows, run the following commands.

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

If you are on macOS or Linux, run the following commands.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install
  1. Run the npm install command.
shell
# install using NPM npm install # or install using YARN yarn install
  1. Rebuild your dependencies.

If you are on Windows, run the following command.

shell
# for Windows .\node_modules\.bin\electron-rebuild.cmd

If you are on macOS or Linux, run the following command.

shell
# for macOS or Linux ./node_modules/.bin/electron-rebuild

You can also rebuild by issuing the following command on macOS or Linux.

shell
$(npm bin)/electron-rebuild

# Delete your node_modules and package-lock.json files

If the error persists, try to delete your node_modules folder and package-lock.json files.

If you are on Windows, open CMD in your project's root directory (where your package.json file is) and issue the following commands.

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

If you are on macOS and Linux, open bash or zsh in your project's root directory and issue the following commands.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

Restart your development server and your IDE and check if the issue is resolved.

# Solve the error when using Docker

If you get the error when using Docker, you need to:

  1. Add a .dockerignore file to ignore your node_modules directory, package-lock.json and yarn.lock files.

  2. When the Docker image builds, it will create the node_modules directory using the currently active version of Node.js and not the version from your personal computer.

# Make sure the Node.js version in your terminal matches your IDE's version

Another thing that may cause the error is if the Node.js version in your code editor (e.g. VS Code) diverges from the Node.js version in your terminal.

You can check your Node.js by running the node -v command in both places.

shell
node -v

If there is a mismatch, try to restart your terminal and make sure the two versions match.

# Install the long-term supported version of Node.js

If the issue persists, try to install the long-term supported version of Node.js.

There are 2 common ways to do this:

  1. Download the LTS version from the official nodejs.org website.

  2. Use the nvm package to manage your Node.js version using a single command.

If you already use nvm to manage your Node.js version, run the following commands to switch to the long-term supported version.

shell
nvm install --lts nvm use --lts

If you don't have nvm installed, follow the instructions in the article that relates to your operating system and install the LTS version.

After you install the LTS version, delete your node_modules directory and reinstall your dependencies as shown in the previous subheading.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev