How to fix npm ERR! gyp ERR! build error npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2 in npm
This error occurs when npm tries to install a package with native C++ code but the build process fails, usually due to missing build tools, Python configuration issues, or version incompatibilities. The `make` command failing with exit code 2 typically indicates missing dependencies or compilation errors.
Quick fix
Fixes gyp ERR!
npm install --verbose 2>&1 | tee build.log- Read the full error output carefully
- Clear npm cache and node_modules
- Install platform-specific build tools
- Verify Python is installed and configured
npm install --verbose 2>&1 | tee build.lognpm ERR! gyp ERR! build errornpm ERR! gyp ERR! stack Error: `make` failed with exit code: 2
npm install --verbose 2>&1 | tee build.logWhat this error means
When npm installs packages containing native C++ code, it uses node-gyp to compile these modules. The error 'make failed with exit code 2' means the build command encountered an unrecoverable error during compilation. This is not an npm issue itself, but rather a problem in the build environment—missing compilers, Python, build tools, or incompatible versions.
Typical symptoms
- npm install fails during package installation with gyp build error
- Installation works on some machines but fails on others
- Error appears when installing packages with native bindings like node-sass, bcrypt, or sqlite3
- The error message is preceded by compilation errors or missing tool messages
- CI/CD pipelines fail at the npm install step
Common causes
- Missing or outdated build tools (make, g++, clang) on your system
- Python not installed or not configured correctly (node-gyp requires Python)
- Incompatible versions of Node.js and node-gyp
- Corrupted node_modules or package-lock.json file
- Missing Visual C++ Build Tools on Windows
- Xcode Command Line Tools not installed on macOS
- Using deprecated packages like node-sass instead of modern alternatives
How to fix "npm ERR! gyp ERR! build error npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2"
The most important step is to look at the error messages ABOVE the 'make failed' line. The actual error is usually printed first.
npm install --verbose 2>&1 | tee build.logSearch the log for the first error message.
Start with a clean slate:
macOS/Linux:
rm -rf node_modules package-lock.json
npm cache clean --force
npm installWindows:
rd /s /q "node_modules"
del package-lock.json
npm cache clean --force
npm installNode-gyp requires a C/C++ compiler.
Windows (PowerShell as Administrator):
npm install --global --production windows-build-toolsmacOS:
xcode-select --installLinux (Ubuntu/Debian):
sudo apt-get update
sudo apt-get install build-essential python3 make g++Node-gyp requires Python. Verify it's installed:
python3 --versionConfigure npm to use it:
npm config set python /usr/bin/python3Update to fix compatibility issues:
npm install -g npm@latest
npm install -g node-gyp@latestSome packages are known to cause gyp errors:
If using node-sass:
npm uninstall node-sass
npm install sass --save-devIf using bcrypt:
npm uninstall bcrypt
npm install bcryptjs --saveThese alternatives don't require compilation.
Advanced notes
Exit code 2 from make is generic; the actual error is always printed above it. Common hidden errors include missing Python, missing node headers, or compilation warnings treated as errors. On CI/CD systems, install build tools in the pipeline BEFORE npm install. Some packages offer environment variables to skip native build—check the package README.
Frequently asked questions
- What does "gyp ERR!" mean?
- When npm installs packages containing native C++ code, it uses node-gyp to compile these modules. The error 'make failed with exit code 2' means the build command encountered an unrecoverable error during compilation. This is not an npm issue itself, but rather a problem in the build environment—missing compilers, Python, build tools, or incompatible versions.
- How do I fix "gyp ERR!"?
- This error occurs when npm tries to install a package with native C++ code but the build process fails, usually due to missing build tools, Python configuration issues, or version incompatibilities. The make command failing with exit code 2 typically indicates missing dependencies or compilation errors.
- What causes "gyp ERR!"?
- Missing or outdated build tools (make, g++, clang) on your system Python not installed or not configured correctly (node-gyp requires Python) Incompatible versions of Node.js and node-gyp
- Is the "gyp ERR!" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- 📚npm Official Documentation
- 📖node-gyp GitHub Repository(Official Docs)
- 💬Stack Overflow: node-gyp Build Issues(Stack Overflow)
- 🐙node-gyp GitHub Issues(GitHub)