I recently upgraded bcryptjs
to version ^3.0.0
in my Node.js project and encountered the following error while trying to hash a password:
const bcrypt = require("bcryptjs");
const password = "mypassword";
const hashedPassword = bcrypt.hashSync(password, 10);
console.log(hashedPassword);
Error:
Error: Neither WebCryptoAPI nor a crypto module is available. Use bcrypt.setRandomFallback to set an alternative
What I Tried:
- I checked if the
crypto
module is available in my Node.js environment. - I attempted to set a fallback using
bcrypt.setRandomFallback(() => require("crypto").randomBytes(16));
, but the issue persisted.
Node.js version is v18.18.0
I recently upgraded bcryptjs
to version ^3.0.0
in my Node.js project and encountered the following error while trying to hash a password:
const bcrypt = require("bcryptjs");
const password = "mypassword";
const hashedPassword = bcrypt.hashSync(password, 10);
console.log(hashedPassword);
Error:
Error: Neither WebCryptoAPI nor a crypto module is available. Use bcrypt.setRandomFallback to set an alternative
What I Tried:
- I checked if the
crypto
module is available in my Node.js environment. - I attempted to set a fallback using
bcrypt.setRandomFallback(() => require("crypto").randomBytes(16));
, but the issue persisted.
Node.js version is v18.18.0
Share Improve this question asked Feb 15 at 8:49 Nijat AliyevNijat Aliyev 89411 silver badges19 bronze badges1 Answer
Reset to default 1Solution:
I downgraded bcryptjs
to version ^2.4.3
, and the issue was resolved:
npm install [email protected]
Now, password hashing works without errors.
Question:
- Why does
bcryptjs
v3.0.0 require WebCryptoAPI or an external crypto module, while v2.4.3 works fine? - Is there a proper way to make it work with v3.0.0 without downgrading?
Hope this helps others facing the same issue!