95

I am getting error Cannot find module 'bcrypt' in nodejs application

I have tried to install it using npm install bcrypt but still getting the issue.

node app.js

Error message:

Dec 30 2015 5:22:18 PM+05:30 - info: Connected to database:  
postgres://testdb:see2@$W@localhost/testdb

Dec 30 2015 5:22:18 PM+05:30 - error: Error: Cannot find module 'bcrypt'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (D:\...\server\modules\user\model
s\user.js:11:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
1

42 Answers 42

102

The solution for me was to npm rebuild.

Sign up to request clarification or add additional context in comments.

Comments

100

Using npm install bcrypt command can't resolve the issue for me.

I tried the commands below and my issue resolved.

npm install node-gyp -g
npm install bcrypt -g

npm install bcrypt --save

3 Comments

node-gyp was giving me issues. Followed the steps above and voila! Thanks buddy.
If you get access error when installing bcrypt, try this : sudo npm install --unsafe-perm --verbose -g bcrypt
It should be npm install bcrypt --save. Works for me!
31

use bcryptjs instead bcrypt this is worked for me

npm install bcryptjs --save

2 Comments

i still get same error with that: SyntaxError: Named export 'compare' not found. The requested module 'bcryptjs' is a CommonJS module,
If you're using TS you also need to install @types/bcryptjs as dev dependency, remember remove @types/bcrypt
26

The Solution is pretty basic, I've solved this Error / Bug with the following steps:

Step 1: Uninstall the bcrypt package with this command :

npm uninstall bcrypt

Step 2: Then ReInstall it :

npm install bcrypt

2 Comments

Wow! It saves me a lot of time! It actually works! Thanks! :)
This simple approach worked perfectly! . Thank you.
18

It should be npm install bcrypt --save. Works for me!

And, if you have others issues after install it, you can check your packages with npm-check.

Comments

10

Solution 1: lengthy method is : Install all dependencies first.

npm install -g windows-build-tools, npm install -g node-gyp

then, install bcrypt : npm install bcrypt

Solution 2: easy method. No dependencies installation required.

npm install bcryptjs

...You might have installed bcrypt but it seems like the installation was not successful for some reason. check the package.json file. If you cannot find bcrypt, the installtion was unsuccessful. You have to install again.

As everyone explained, it because of lack dependencies that your installation was unsuccessful. You can checkout the required dependencies in the link: https://www.npmjs.com/package/bcrypt

Note: To use bcrypt: var bcrypt = require('bcrypt'); .....

to use bcryptjs. var bcrypt = require('bcryptjs');

for reference: https://www.npmjs.com/package/bcrypt https://www.npmjs.com/package/bcryptjs

2 Comments

what's the second way?
does it (the first solution) work on windows only? cause I see you installed windows-build-tools
8

Before using npm install, change the package.json file dependencies, i.e.

"bcrypt":"0.7.6" 

to

"bcrypt":"*"

Comments

8

This happened to me as I was installing a package from github that had an older version of bcrypt as a dependency.

Just uninstall bcrypt to clear out the old version and install a new version:

npm uninstall bcrypt
npm install bcrypt

1 Comment

I would like to note to keep an eye on the module that had the older bcrypt to ensure using a newer version didn't break anything. If it did, you may have to update that dependency, if possible.
7

This worked for me.

1) Delete any bcrypt folder in nodemodules folder, folder may have been created due to your repeated tries. (C:\Program Files\nodejs\node_modules\npm\node_modules)

2) run this code npm install --save bcryptjs e.g -

C:\Projects\loginapp>npm install --save bcryptjs 

1 Comment

Thank you! This worked for me. You need to delete old files!!!
4

In my case, npm rebuild alone didn't solved it. I also had to:

$ npm install -g node-gyp
$ sudo apt-get update
$ sudo apt-get install build-essential
$ npm rebuild

npm rebuild was trying to run make.

Comments

3

It seems that bcrypt was depreciated at version 1.0.3 as it was susceptible to a wrap-around bug. NPM recommends installing version 2.0.0.

So, if you wish to save it, just run the command:

npm install [email protected] --save

Comments

3

I'm using bcrypt with typescript

npm i --save @types/bcryptjs

Helped me solve the error above.

Comments

2

If none of these examples didn't work, you should try to downgrade Node version installed:

E.g from Node version 10 to version 9

npm install node@<version of node>

4 Comments

I had trouble with node 12. I downgraded to node 10 and removed the node_modules folder. After reinstalling, it worked!
@joeytwiddle If even that won't work, the best solution is to install bcryptjs. It has zero dependencies and it's not influenced by OS used.But first it needs to be uninstalled bcrypt.
I was trying to run this github.com/komarserjio/notejam The express version over node 12. I've tried this solution downgrading to node v10 and it worked! thanks.
One more tip would be to use bcryptjs instead of bcrypt. Bcrypt has a problem with dependencies very often
1

I'm running Ubuntu 16.04 on DigitalOcean (512 MB / 1 CPU, 20 GB SSD)

The following worked for me:

  1. Scale your droplet up to the 1 GB RAM option ($10/mo)

  2. Run each of the following commands (one at a time)

    sudo npm install node-gyp -g
    sudo apt-get install python
    sudo apt-get install make
    sudo apt-get install g++
    
  3. Then try again with:

    npm install bcrypt --save
    
  4. Scale droplet back down to the 512 MB option

Comments

1

You need to update the g++ compiler version in your linux system. To update the compiler just run the commands below:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test

sudo apt-get update

sudo apt-get install gcc-4.9 g++-4.9

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9


npm install bcrypt --save

Comments

1

I followed some course, and for me it didn't work. My mistake was:

var bcrypt = require('bcrypt.js'); 

But when I changed it to

var bcrypt = require('bcryptjs');

It worked!

Comments

1
  • Node Version vs Bcrypt Version
  • 0.4 <= 0.4
  • 0.6, 0.8, 0.10 >= 0.5
  • 0.11 >= 0.8
  • 4 <= 2.1.0
  • 8 >= 1.0.3
  • 10, 11 >= 3
  • 12 >= 3.0.6

I had the same issue, after I installed the bcrypt particular version depends on your node version, it started to working.

In my case my nodeJS version was 12.3.0 , so I installed by specifying the version " npm install [email protected]."

I hope it will resolve the issue.

Comments

1

For me, it was because bcryptjs has a different name.

To fix it simply,

  • cd node_modules Go to node_modules folder on your application
  • ln -s bcryptjs bcrypt OR for Windows mklink /D bcryptjs bcrypt

This creates a link of bcryptjs directory to bcrypt.

Comments

1

if your issue didn't resolved. The below syntax will surely provide you a solution. To use bcrypt: var bcrypt = require('bcrypt'); To use bcryptjs. var bcrypt = require('bcryptjs');

Comments

1

Mostly useful when using pnpm as per new Next.js official tutorial.

Fixing bcrypt Module Error in Next.js Issue If you encounter the following error during the Next.js build:

Error: Cannot find module 'bcrypt'

This happens because bcrypt requires native dependencies that may not be compatible with your environment. A simpler solution is to use bcryptjs, which is a pure JavaScript implementation. Solution Follow these steps to fix the issue:

  1. Uninstall bcrypt and Install bcryptjs
Using pnpm
pnpm remove bcrypt && pnpm add bcryptjs
Using npm
npm uninstall bcrypt && npm install bcryptjs
  1. Update Import Statements In your project files, replace:
import bcrypt from 'bcrypt';

with:

import bcrypt from 'bcryptjs';
  1. Restart and Build the Project Run the following commands:
pnpm run dev  # or npm run dev
pnpm run build

This should resolve the issue, as bcryptjs does not require native dependencies and works seamlessly with Next.js.

Comments

1

In my case, the error ocurred during build time, in a Next.js application. The command bellow solved the issue.

pnpm approve-builds

Regards,

Comments

1

I had this issue only when working with Docker and Nestjs, above solutions didn't worked. This one eventually helped:

in package.json:

"pnpm": {
    "onlyBuiltDependencies": ["bcrypt"]
},

in Dockerfile:

RUN pnpm install
RUN pnpm rebuild bcrypt

1 Comment

Welcome to StackOverflow. As you are a new contributor of knowledge, just a small feedback on your answer: As answers can pile up quite a bit, it's helpful to add the specifics about what solution you are talking about that didn't work for you. It makes your answer future proof. Also, I can't see the connection between the question and NestJs. It seems as a redundant info and might even show that the issue you had, was in some sort of different context than the context the question setting is in. Other than that, keep up the good work!
1

after pnpm i bcrypt, use pnpm approve-builds and choose bcrypt

$ pnpm approve-builds
√ Choose which packages to build (Press <space> to select, <a> to toggle all, <i> to invert selection) · bcrypt
√ The next packages will now be built: bcrypt.
Do you approve? (y/N) · true
node_modules/.pnpm/[email protected]/node_modules/bcrypt: Running install script, done in 2.9s

Comments

0

I can't run any npm commads. so, I download from this link https://github.com/kelektiv/node.bcrypt.js create folder bcrype and use it. Solve now.

Comments

0

For me the issue resolved by below steps: Nothing above solved my issue, 1) rm -rf node_modules in your project directory 2) rm package-lock.json 3) just check you have your package.json file now 4) npm install

Thats it, you will get bcrypt installed properly. Hope this helps.

Comments

0

This worked for me:

npm install bcryptjs

Then:

npm update

Comments

0

First delete bcrypt module from your node modules. Then try the below steps:

1) npm install node-gyp -g

2) npm install bcrypt -g

3) npm install bcrypt -save

This will definitely resolve the issue.

2 Comments

"--save" not "-save". Also you can do npm install bcrypt -g --save
this is not working for me node v8.16.1 npm 6.4.1 npm install node-gyp -g npm install bcrypt -g npm install bcrypt --save
0

Be sure you are in a stable version of node too. If you are working with n, you only need to:

sudo n stable

And then again:

npm install bcrypt --save

And it worked for me.

Comments

0

If this is an error you are facing when using something like Travis CI, consider using npm install --build-from-source.

Comments

0

The problem could be because there is no this essential

sudo apt-get install -y build-essential python

Then agregate bcrypt with if you're using npm:

npm install bcrypt
npm rebuild

or if you're using yarn:

yarn add bcrypt
yarn install
yarn build

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.