bcryptjs is an optimized bcrypt implementation in pure JavaScript with zero dependencies. It provides an API compatible with the native C++ `bcrypt` module but ensures maximum portability across Node.js and browser environments without requiring compilation tools like `node-gyp`. While highly convenient and stable, the pure JS implementation is roughly 30% slower than the native binding.
npm install bcryptjsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the standard, secure way to hash a password with a generated salt, and then securely compare a plaintext login attempt against the stored hash using asynchronous methods.
Always use the asynchronous methods (`hash`, `compare`, `genSalt`) returning Promises or accepting callbacks in server-side code to prevent DoS-like freezes under load.
If your application needs to support extremely long passwords or multi-byte characters that exceed 72 bytes, pre-hash the password using a fast cryptographic hash (like SHA-256 or SHA-512) before passing it to `bcryptjs`.
Balance security and performance by selecting an appropriate salt round cost (typically 10 to 12 for modern web applications). If performance is a critical bottleneck, migrate to the native `bcrypt` package.
Install the community-maintained DefinitelyTyped definitions by running `npm install --save-dev @types/bcryptjs` or `yarn add -D @types/bcryptjs`.
Refactor the authentication logic to use `await bcrypt.compare(password, hash)` and `await bcrypt.hash(password, salt)`.
Ensure both arguments passed to `bcrypt.compare()` are valid strings. Always check if the user and their stored hashed password exist before attempting a comparison: `if (!user || !user.passwordHash) return false;`.
No dependency data recorded yet.