node-gyp Build Failed — Native Addon Cannot Compile
Repair node-gyp failures from native addons by checking Python, a C++ toolchain, node headers, and the gyp configuration that owns the build error.
Symptoms
- npm install fails with "gyp ERR! build error" and exits non-zero
- A transitive dependency such as a database or crypto addon reports node-gyp errors
- Works on a developer machine but fails in a slim CI container
Possible causes
- No C++ compiler or make is present in the build environment
- The Node headers for the running Node ABI version are not downloaded or are blocked by a proxy
- Python is missing or the detected python points at Python 3.12 where distutils was removed
Diagnosis steps
Reproduce the failure with verbose gyp output
npm install --verbose 2>&1 | grep -A 20 "gyp ERR!"Interpretation
The first gyp ERR! block names the real cause: missing compiler, missing headers, or a Python error.
Verify the toolchain is present
cc --version && make --version && python3 --versionInterpretation
Any missing binary explains a build error. On Debian slim images install build-essential and python3.
Point node-gyp at explicit headers
npm config set nodedir /path/to/node && node -p "process.versions.modules"Interpretation
Matching headers to the ABI version (process.versions.modules) resolves download failures in offline networks.
Root cause
Native addons compile C++ against Node headers via node-gyp. The build fails whenever the compiler, the Python build shim, or the ABI-matched headers are unavailable, because node-gyp cannot complete the configure and make steps.
Fix
Install the build toolchain (build-essential, python3, make, g++), match Node header download settings, and pin prebuilt binaries where possible with npm config set prefer-offline or a prebuild-install override.
Risk note
Prebuilt binaries avoid compilation but must match the platform and ABI; a mismatched prebuild fails at runtime, not build time.
Verify the fix
Rerun npm install in a clean environment and confirm the addon builds or a prebuild downloads. Then require the module from Node and call one method to prove the binary loads.
Symptoms
This debugging guide addresses “node-gyp Build Failed — Native Addon Cannot Compile”. Start from the symptoms, follow the ordered diagnostics, and confirm each hypothesis before moving on; the fix and verification steps then close the loop.
Observed symptoms include:
- npm install fails with “gyp ERR! build error” and exits non-zero
- A transitive dependency such as a database or crypto addon reports node-gyp errors
- Works on a developer machine but fails in a slim CI container
Reproduce and Diagnose
Reproduce the failure in isolation before changing any code. Work through the checks below in order; each one narrows the search space.
Step 1: Reproduce the failure with verbose gyp output
npm install --verbose 2>&1 | grep -A 20 "gyp ERR!"
The first gyp ERR! block names the real cause: missing compiler, missing headers, or a Python error.
Step 2: Verify the toolchain is present
cc --version && make --version && python3 --version
Any missing binary explains a build error. On Debian slim images install build-essential and python3.
Step 3: Point node-gyp at explicit headers
npm config set nodedir /path/to/node && node -p "process.versions.modules"
Matching headers to the ABI version (process.versions.modules) resolves download failures in offline networks.
Likely contributors, in order of frequency:
- No C++ compiler or make is present in the build environment
- The Node headers for the running Node ABI version are not downloaded or are blocked by a proxy
- Python is missing or the detected python points at Python 3.12 where distutils was removed
Root Cause
Native addons compile C++ against Node headers via node-gyp. The build fails whenever the compiler, the Python build shim, or the ABI-matched headers are unavailable, because node-gyp cannot complete the configure and make steps.
The Fix
Install the build toolchain (build-essential, python3, make, g++), match Node header download settings, and pin prebuilt binaries where possible with npm config set prefer-offline or a prebuild-install override.
Watch out: Prebuilt binaries avoid compilation but must match the platform and ABI; a mismatched prebuild fails at runtime, not build time.
# Debian/Ubuntu build image
apt-get update && apt-get install -y build-essential python3 make g++
# Force header download and retry
npm install --build-from-source --verbose
# Or prefer prebuilt addons
npm config set prefer-offline true
Verify and Prevent
Rerun npm install in a clean environment and confirm the addon builds or a prebuild downloads. Then require the module from Node and call one method to prove the binary loads.
Once verified, add a regression test that reproduces the original symptom so the failure cannot silently return.
This class of failure is usually environmental rather than pure logic, so capture the exact command, the environment variables, and the working directory in your bug report. Reproduce it in the smallest possible script, then widen the scope only once the minimal case is understood. A short written checklist for this failure mode saves hours the next time it appears.