Fix 'gyp ERR! build error' and 'node-gyp rebuild failed' (bcrypt, sharp, sqlite3, canvas)
Install the native build toolchain (Python, make, C++ compiler) or switch to prebuilt / pure-JS alternatives to stop node-gyp failures.
gyp ERR! build error gyp ERR! stack Error: `make` failed with exit code: 2 npm ERR! code 1 npm ERR! command sh -c node-gyp rebuild
# Install the native toolchain for ubuntu sudo apt-get install -y python3 make g++ build-essential # Clean and reinstall rm -rf node_modules package-lock.json npm install # Prefer prebuilt / pure-JS packages when possible: # bcrypt -> bcryptjs # sqlite3 -> better-sqlite3 (prebuilt) or node:sqlite (Node 22+)
Root Cause & Mechanism Analysis
Why does this error occur?
A dependency ships native C/C++ addons and no prebuilt binary matches your Node.js version, OS, or CPU architecture, so npm compiles it from source. Compilation fails when Python 3, make, or a C++ compiler is missing, or when the package does not support your Node major version yet.
Frequently Asked Questions
Why does it work locally but fail in Docker?
Need Zero-Knowledge .env Secret Sync Across Your Engineering Team?
Explore Castov Env Vault for client-side AES-256-GCM encrypted environment sharing, or sponsor this engineering hub.
Related Developer Error Solutions
Browse All 69 GuidesNPM: ERESOLVE unable to resolve dependency tree
Resolve strict peer dependency conflicts in npm 7+ using --legacy-peer-deps or updating outdated packages.
Docker: exec user process caused: exec format error
Resolve CPU architecture mismatches when building Docker images on Apple Silicon (M1/M2/M3/M4 `linux/arm64`) and deploying to x86_64 (`linux/amd64`) cloud servers.
npm ERR! code EACCES: permission denied, mkdir '/usr/local/lib/node_modules'
Stop using sudo npm. Reconfigure npm's global prefix or use a Node version manager so global installs and npx work without root.