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 ERR! code ENOAUDIT npm ERR! Audit endpoint not supported
How to fix "npm ERR! code ENOAUDIT - Audit endpoint not supported"
npm ERR! code EBADDEVENGINES npm ERR! devEngines.runtime incompatible with current node version
How to fix "npm ERR! code EBADDEVENGINES - devEngines.runtime incompatible with current node version"
npm ERR! code ETOOMANYARGS npm ERR! Too many arguments
How to fix "npm ERR! code ETOOMANYARGS - Too many arguments"
npm ERR! code EINVALIDTAGNAME npm ERR! Invalid tag name: tag names cannot contain spaces
How to fix "npm ERR! code EINVALIDTAGNAME - tag names cannot contain spaces"
npm ERR! code E400 npm ERR! 400 Bad Request
How to fix "npm ERR! code E400 - 400 Bad Request" error