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.
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.
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.
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.
npm ERR! code E401 npm ERR! 401 Unauthorized - Token has expired
Token has expired - npm authentication failure
npm ERR! code EAI_NODATA npm ERR! errno EAI_NODATA npm ERR! getaddrinfo EAI_NODATA registry.npmjs.org
How to fix "npm ERR! code EAI_NODATA - getaddrinfo EAI_NODATA"
npm ERR! code EMPTYPACKAGE npm ERR! Package contains no files
How to fix 'npm ERR! code EMPTYPACKAGE' - Package contains no files
npm ERR! code EWORKSPACEMISSING npm ERR! Workspace does not exist: packages/missing
How to fix "npm ERR! code EWORKSPACEMISSING - Workspace does not exist" error
npm ERR! code EADDRNOTAVAIL npm ERR! errno EADDRNOTAVAIL npm ERR! Address not available
How to fix "npm ERR! code EADDRNOTAVAIL - Address not available" error