219

I am running a node application on terminal. Have recently upgraded to node v8.5.0, but am getting this error:

Error: The module '/tidee/tidee-au/packages/tidee-au-server/node_modules/bcrypt/lib/binding/bcrypt_lib.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 51. This version of Node.js requires
NODE_MODULE_VERSION 57. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at Object.Module._extensions..node (module.js:653:18)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/tidee/tidee-au/packages/tidee-au-server/node_modules/bcrypt/bcrypt.js:6:16)
    at Module._compile (module.js:624:30)
    at Module._extensions..js (module.js:635:10)
    at Object.require.extensions.(anonymous function) [as .js] (/tidee/tidee-au/packages/tidee-au-server/node_modules/babel-register/lib/node.js:152:7)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/tidee/tidee-au/packages/tidee-au-server/server/helpers/encryptPass.js:1:16)

Any idea how to solve this?

RPichioli
  • 3,245
  • 2
  • 25
  • 29
JoeTidee
  • 24,754
  • 25
  • 104
  • 149

28 Answers28

173

You need to remove the module folder (bcrypt) from the node_modules folder and reinstall it, use the following commands:

$ rm -rf node_modules/bcrypt
$ npm install
// or
$ yarn
Gianfranco P.
  • 10,049
  • 6
  • 51
  • 68
alexmac
  • 19,087
  • 7
  • 58
  • 69
  • 19
    Doesn't work. For me, the problem is with the `zmq` package, and rebuilding or deleting it and `npm installing` all over again doesn't help. Always the same problem. – trusktr May 29 '18 at 06:38
  • 11
    @trusktr Try to delete node_modules folder completely: `rm -rf node_modules`. Clear npm cache: `npm cache clear`, remove package-lock.json or npm-shrinkwrap.json. And only after that run `npm i`. – alexmac Jun 01 '18 at 09:09
  • 3
    Got a nice message, "I sure hope you know what you are doing." when I did that cache clear. Me not so much, but apparently you do! It worked. – ouflak Jul 23 '18 at 07:02
  • you don't/never need to `npm cache clear`, the other commands made it – caub Jul 27 '18 at 07:48
  • Worked for me! My exact error was - "The module '/Users//Downloads/intro-to-graphql/node_modules/bcrypt/lib/binding/bcrypt_lib .node' was compiled against a different Node.js version using NODE_MODULE_VERSION 64." – theusual Apr 16 '19 at 16:25
  • `cache clear` worked for me as well. I switched over between different versions of Node, which might have been a reason. – Adam Libuša Dec 13 '20 at 17:14
  • You may have to do `npm install -g MODULE_NAME` to go global. I did. – Erik Trautman Mar 01 '21 at 13:12
146

I had the same problem and nothing mentioned here worked for me. Here is what worked for me:

  1. Require all dependencies you need in the main.js file that is run by electron. (this seemed to be the first important part for me)
  2. Run npm i -D electron-rebuild to add the electron-rebuild package
  3. Remove the node-modules folder, as well as the packages-lock.json file.
  4. Run npm i to install all modules.
  5. Run ./node_modules/.bin/electron-rebuild (.\node_modules\.bin\electron-rebuild.cmd for Windows) to rebuild everything

It is very important to run ./node_modules/.bin/electron-rebuild directly after npm i otherwise it did not work on my mac.

Dharman
  • 30,962
  • 25
  • 85
  • 135
chitzui
  • 3,778
  • 4
  • 28
  • 38
  • 9
    Just running electron-rebuild did the job for me. No need for deleting and reinstalling modules in my case. – stoefln Jan 23 '19 at 07:54
  • 1
    Well then you got lucky @stoefln unfortunately it is crucial to follow these exact steps one by one in some setups. – chitzui Jan 23 '19 at 14:31
  • 1
    I'm on Windows but this solution worked for me as well. Can you explain what causes this problem? Could this be a bug with Electron? – jbinvnt Jun 01 '19 at 19:39
  • @jbinvnt yes, it is known Electron limitation https://github.com/SimulatedGREG/electron-vue/issues/872 – Konstantin Vdovkin Mar 13 '20 at 14:17
  • How to run only electron-rebuild through pipeline or any command – Kirataka May 05 '20 at 09:19
  • I had the same issue with a Meteor project. I didn't realize there was difference between meteor npm install and npm install. The former ensures that the correct packages that get installed using using the same npm version that has been tested with your current version of Meteor. My understanding is that this is common with other frameworks such as Electron. – CaseyC Nov 11 '20 at 17:18
  • Here is the electron official documentation on this https://www.electronjs.org/docs/tutorial/using-native-node-modules – 4xy Dec 05 '20 at 16:37
  • 1
    just a small remark: the recipe mentioned above assumes that you have installed electron locally! – Andreas Rozek Dec 21 '20 at 09:05
  • Also no need for the first step in my case. Good answer anyway! – Aritz May 09 '22 at 13:34
94

You have to rebuild the package and tell npm to update it's binary too. Try:

npm rebuild bcrypt --update-binary

@robertklep answered a relative question with this command, look.

Only rebuild haven't solved my problem, this works fine in my application.

Dharman
  • 30,962
  • 25
  • 85
  • 135
RPichioli
  • 3,245
  • 2
  • 25
  • 29
  • 1
    i thought that the purpose of rebuild was to update the binary, when would you like to rebuild WITHOUT updating the binary ? – bobmoff Nov 23 '18 at 08:27
  • 1
    @bobmoff I was thinking the same way trying to use only rebuild, the docs says they update everything for us.. but only errors - My problem was to sync my legacy working local repository to a new local NodeJS / NPM version. So I looked around and "--update-binary" was the way to tell it to really force and change it the way it have to do with rebuild naturally. – RPichioli Nov 23 '18 at 17:07
39

Simply run:

npm uninstall bcrypt

Followed by:

npm install bcrypt (or npm install, if bcrypt is declared as dependency in your package.json file)

Yaroslav
  • 4,543
  • 5
  • 26
  • 36
Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
19

you can see this link

to check your node verison right. using NODE_MODULE_VERSION 51 means that your node version is nodejs v7.x, requires NODE_MODULE_VERSION 57 means you need upgrade your node to v8.x,so you need to upgrade your node. and then you need run npm rebuild command to rebuild your project

liming gao
  • 199
  • 1
  • 4
  • 4
    The answer itself should be helpful without the link. – Tobias Wilfert Jan 04 '19 at 08:37
  • 1
    Had a similar problem complaining from 67 to 57. In my case I downgrade from `11/stable` to `8/stable` with `$ snap refresh node --channel=8/stable` in Ubuntu. After that did `$ npm rebuild`. – Daniel Feb 10 '19 at 19:07
  • This answer is wrong. `NODE_MODULE_VERSION 51` means the `.node` file is compiled against some node version using NODE_MODULE_VERSION 51. And the user is using a node version using 57, so he can't use it directly. The user should rebuild the compilation. – alsotang Oct 09 '19 at 14:44
18

Be sure you only have one version of NodeJS installed. Try these two:

node --version
sudo node --version

I initially installed NodeJS from source, but it was the incorrect version and 'upgraded' to the newest version using nvm, which doesn't remove any previous versions, and only installs the desired version in the /root/.nvm/versions/... directory. So sudo node was still pointing to the previous version, whilst node was pointing to the newer version.

Barkles
  • 703
  • 9
  • 12
14

Most likely you have this issue due to the package-lock.json. Somehow it seems to block you from recompiling or rebuilding your dependencies, even if you explicitly run npm rebuild. I ran all the following to fix it for me:

rm package-lock.json;
rm -rf node_modules;
npm install;
George Linardis
  • 130
  • 3
  • 11
smets.kevin
  • 1,570
  • 14
  • 17
4

I deleted the node_modules folder and run npm install and my application started without any errors.

Dushan
  • 1,365
  • 20
  • 26
3

I got the same error but I was trying to run a node application using a Docker container.

I fixed it by adding a .dockerignore file to ignore the node_modules directory to make sure that when the docker image builds, it builds the native packages for the image I wanted (Alpine) instead of copying over the node_modules compiled for my host (Debian).

Matt
  • 430
  • 6
  • 8
3

Turns out my problem was user-error: make sure the version of node you are using for running is the same that you are using when running an npm install or yarn.

I use NVM for versioning node and was running yarn via a terminal, but my IDE was set to use an older version of node when running and it was throwing the error above. Matching my IDE's version of node in the run config to node --version fixed the issue.

Craig Odell
  • 133
  • 1
  • 4
2

Here is what worked for me. I am using looped-back node module with Electron Js and faced this issue. After trying many things following worked for me.

In your package.json file in the scripts add following lines:

  ... 
"scripts": {
        "start": "electron .",
        "rebuild": "electron-rebuild"
    
      },
...

And then run following command npm run rebuild

sediq khan
  • 515
  • 4
  • 10
1

I got this error when running my app with systemd:

ExecStart=/usr/local/bin/node /srv/myapp/server.js

But I was using a different version for npm install in the shell:

$ which node
/home/keith/.nvm/versions/node/v8.9.0/bin/node

If this is your setup, you can either hardcode the node version in the service file or follow a workaround like this one.

ki9
  • 5,183
  • 5
  • 37
  • 48
1

I had the same problem and none of these solutions worked and I don't know why, they worked for me in the past for similar problems.

Anyway to solve the problem I've just manually rebuild the package using node-pre-gyp

cd node_modules/bcrypt
node-pre-gyp rebuild

And everything worked as expected.

Hope this helps

wezzy
  • 5,897
  • 3
  • 31
  • 42
1

I had a similar problem with robotjs. There were some deprecated code that required node v11, but I had already compiled electron code on v12. So I got basically the same error. Nothing here worked as I was basically trying to rebuild electron and my other dependencies into node v11 from v12.

Here is what I did (part of this is based on chitzui's answer, credit where credit is due):

  • Back up package.json
  • completely delete the node_modules folder
  • completely delete package_lock.json
  • delete package.json (will reinit later)
  • Close any open editors and other cmd windows that are in the project's directory.
  • run npm init to reinit package, then missing data with old backed up package.json
  • run npm i
  • fixed
Dharman
  • 30,962
  • 25
  • 85
  • 135
AirFusion
  • 23
  • 1
  • 5
1

you need just run this below commands:

$ rm -rf node_modules
$ rm -rf yarn.lock
$ yarn install

and finally

$ ./node_modules/.bin/electron-rebuild

don't forget to yarn add electron-rebuild if it doesn't exist in your dependencies.

Ehsan
  • 3,711
  • 27
  • 30
1

For Electron modules, install electron-rebuild.

Format:
electron-rebuild -o <module_name> -v <electron version>

Example:
electron-rebuild -o myaddon -v 9.0.0-beta.6

Specify the same version that you have installed in the current directory

You might have this experience where a standard node-gyp build would report as 64, then a basic electron-rebuild would report 76, not until you add -v with exact version it bumps to actual version 80 (for 9.0.0-beta.6)

tomm1e
  • 143
  • 1
  • 7
1

this is occoures because you currently change your node js version, just run in terminal in your project

$ rm -rf node_modules/bcrypt

then reinstall

$ npm install

you can start it. ok

gentur ariyadi
  • 2,105
  • 1
  • 10
  • 10
0

After trying different things. This worked.

Delete your node modules folder and run

npm i
4b0
  • 21,981
  • 30
  • 95
  • 142
0

I faced the same issue with grpc module and in my case, I was using electron and have set a wrong electron version in the env variable "export npm_config_target=1.2.3", setting it to the electron version I am using resolved the issue on my end. Hope this helps someone who set env variables as given here (https://electronjs.org/docs/tutorial/using-native-node-modules#the-npm-way)

kohl
  • 566
  • 2
  • 9
  • 21
0

You could remove bcrypt entirely and install bcryptjs. It is ~30% slower, but has no dependencies, so no pains installing it.

npm i -S bcryptjs && npm uninstall -S bcrypt

We've installed it successfully for our applications. We had issues with bcrypt not compiling on AWS instances for Node v8.x

Daniel Nitu
  • 387
  • 3
  • 8
0

Potentially, inconsistency of the node JS versions is what causes the problem. As stated in the documentation. Be sure to use one of the lts release. E.g. specify this in your Dockerfile:

# Pull lts from docker registry
FROM node:8.12.0

# ...
kmos.w
  • 422
  • 1
  • 3
  • 13
0

I just got this error running kadence the installed "kadence" script checks for nodejs first and only runs node if there is no nodejs. I have the latest version of node linked into my ~/bin directory but nodejs runs an older version that I had forgotten to uninstall but never caused problems until just now.

So people with this problem might check if node and nodejs actually run the same version of node...

Bill Burdick
  • 935
  • 10
  • 15
0

In my case, I was in my office proxy which was skipping some of the packages. When I came out of my office proxy and tried to do npm install it worked. Maybe this helps for someone.

But it took me several hours to identify that was the reason.

Balasubramanian S
  • 1,345
  • 12
  • 16
0

In my case I was running nodejs instead of node. Due to nodejs being installed by the package manager:

# which node
/home/user/.nvm/versions/node/v11.6.0/bin/node

# which nodejs
/usr/bin/nodejs
benathon
  • 7,455
  • 2
  • 41
  • 70
0

run npm config set python python2.7 and run npm install again the party is on.

0

I have hit this error twice in an electron app and it turned out the problem was that some modules need to be used from the main process rather than the render process. The error occurred using pdf2json and also node-canvas. Moving the code that required those modules from index.htm (the render process) to main.js (the main process) fixed the error and the app rebuilt and ran perfectly. This will not fix the problem in all cases but it is the first thing to check if you are writing an electron app and run into this error.

Prajna
  • 11
  • 2
0

I came here because I was getting this error for the quokka.js ext in vscode.

My solution: (on a mac via the terminal)

1- I went to ~/.quokka

2- I ran nano config.json

3- I copied the code from config.json into a separate file

4- I deleted the code in config.json

5- I stopped and restarted Quokka.

6- Once I confirmed that Quokka was working without errors, I deleted the config.json file code.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

I had a similar error:

...
was compiled against a different Node.js version using
NODE_MODULE_VERSION 93. This version of Node.js requires
NODE_MODULE_VERSION 83. Please try re-compiling or re-installing
...

I use nvm and I had been switching between node versions.

I got this error on v16.17.1.

The only thing that worked was:

Switching back to 14, installing node modules, switching to 16, and installing node modules again.

nvm use v14.18.2
npm i
nvm use v16.17.1
npm i

UPDATE: The error reappeared. So then I deleted node_modules and ran npm i again. I'm not exactly sure about all the steps I took. this is one of those weird issues for me that I can't quite pin down what fixes it.

Dashiell Rose Bark-Huss
  • 2,173
  • 3
  • 28
  • 48