bcrypt.js Password Hashing
bcrypt.js is an optimized implementation of the bcrypt password hashing function in pure JavaScript, providing zero dependencies and full TypeScript support. Compatible with the C++ bcrypt binding, it is currently stable at version 3.0.3 and is actively maintained with releases as needed to address bugs and modernize the codebase.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax in an ES Module context (e.g., in a modern Node.js project with `"type": "module"` or a browser module).fixUpdate your import statement from `const bcrypt = require('bcryptjs')` to `import bcrypt from 'bcryptjs'`. -
Error: data and salt arguments required by bcrypt.hash.
cause `bcrypt.hash` was called with an insufficient number of arguments, typically missing the `salt` or `rounds` parameter.fixEnsure `bcrypt.hash` is called with at least two arguments: the password (`data`) and either a generated `salt` or the number of `rounds` for salt generation (e.g., `bcrypt.hash(password, 10)`). -
TypeError: Cannot read properties of undefined (reading 'then')
cause This usually occurs when attempting to `await` or `.then()` on an `bcrypt` asynchronous function (like `bcrypt.hash`) that returned `undefined` because it was called with an incorrect number of arguments, implicitly switching it to a callback-based API (if applicable) or failing to return a promise.fixVerify that asynchronous `bcrypt` functions are called with the correct arguments to return a promise (e.g., `await bcrypt.hash(password, 10)`) or, if using callbacks, that the callback is correctly provided (e.g., `bcrypt.hash(password, 10, (err, hash) => {})`).
Warnings
- breaking Since v3.0.0, bcrypt.js exports an ECMAScript module (ESM) by default. Projects using CommonJS `require()` will need to update to `import` syntax or configure their bundler/Node.js environment accordingly.
- breaking Version 3.0.0 changed the default hash generation to produce 2b-style hashes. While this library was not affected by the original 2a/2b bug, the output format for newly generated hashes has changed.
- gotcha As a pure JavaScript implementation, `bcryptjs` is approximately 30% slower than the native C++ bcrypt binding available on Node.js. This affects the number of iterations that can be processed in a given time.
- gotcha The maximum input password length for bcrypt.js is 72 bytes. Passwords exceeding this length are not implicitly truncated or validated by the library itself.
- gotcha When using the ESM variant of bcrypt.js directly in a browser without a bundler, the `crypto` import may need to be stubbed out (e.g., via an import map) to prevent browser-specific errors.
Install
-
npm install bcryptjs -
yarn add bcryptjs -
pnpm add bcryptjs
Imports
- bcrypt
const bcrypt = require('bcryptjs')import bcrypt from 'bcryptjs'
Quickstart
import bcrypt from "bcryptjs";
async function main() {
const password = "mySecretPassword123";
// Auto-generate a salt with 10 rounds and hash the password
const hash = await bcrypt.hash(password, 10);
console.log("Hashed password:", hash);
// Compare a password against the stored hash
const isMatch = await bcrypt.compare(password, hash);
console.log("Password matches:", isMatch); // true
const notMatch = await bcrypt.compare("wrongPassword", hash);
console.log("Wrong password matches:", notMatch); // false
}
main();