This error occurs when bcrypt can't find prebuilt binaries and attempts to compile from source without necessary build tools. Install build dependencies or consider using bcryptjs as a pure JavaScript alternative.
bcrypt is a password hashing library with native C++ bindings for performance. It first tries to download prebuilt binaries for your platform. When that fails, it falls back to building from source using node-pre-gyp. This error occurs when both approaches fail - usually because build tools (gcc, g++, make, python) are missing from your system. bcrypt requires native compilation because the computationally intensive hashing algorithm is implemented in C++ for security and performance reasons.
Install the required build dependencies:
sudo apt-get update
sudo apt-get install -y build-essential python3This includes gcc, g++, make, and other compilation tools.
For Red Hat-based systems:
sudo yum groupinstall -y "Development Tools"
sudo yum install -y python3For Alpine Linux (Docker):
apk add --no-cache build-base python3After installing build tools:
npm install bcryptOr rebuild existing installation:
npm rebuild bcryptIf native compilation is problematic, use the pure JavaScript version:
npm uninstall bcrypt
npm install bcryptjsUpdate your imports:
// Before
const bcrypt = require('bcrypt');
// After (API compatible)
const bcrypt = require('bcryptjs');bcryptjs is slower but has no native dependencies.
For Docker builds, add build tools in a multi-stage build:
FROM node:20-alpine AS builder
RUN apk add --no-cache build-base python3
WORKDIR /app
COPY package*.json ./
RUN npm ci
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY . .bcrypt vs bcryptjs comparison:
- bcrypt: ~100x faster, requires compilation
- bcryptjs: Pure JS, no native deps, slower but acceptable for most apps
For CI/CD pipelines, caching the node_modules after successful bcrypt compilation can save time on subsequent runs.
If you're using an uncommon platform/architecture, bcrypt may not have prebuilt binaries. Check https://github.com/kelektiv/node.bcrypt.js for supported platforms.
Set the Python path explicitly if needed:
npm config set python /usr/bin/python3npm notice access token expired or revoked. Please try logging in again.
Token has expired - npm authentication failure
npm ERR! code EAI_AGAIN
How to fix "EAI_AGAIN" in npm
npm error code E403 npm error 403 Forbidden - PUT https://registry.npmjs.org/<package>
How to fix 'E403 Forbidden' error in npm
npm ERR! code EUSAGE npm ERR! Usage error
How to fix "npm ERR! code EUSAGE" in Node.js projects
npm ERR! code E401 npm ERR! 401 Unauthorized
How to fix "E401 Unauthorized" in npm