npm node-gyp build failed — how to fix

# Identify the tool that failed before changing dependencies
node --version
npm --version
python3 --version
make --version
c++ --version

# macOS
xcode-select --install

# Ubuntu/Debian
sudo apt install -y python3 make g++

# Windows: install Python and Visual Studio
# with the "Desktop development with C++" workload
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! System    Darwin 23.0.0
gyp ERR! command   "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "build" "--fallback-to-build"

node-gyp compiles native Node.js addons. A failure can occur while locating Python, generating build files, compiling C/C++ code, or downloading headers. The final npm ERR! code 1 is only the result; the first gyp ERR! message identifies the repair.

Find the first failing tool

Read upward from the final npm error. Messages about find Python indicate a Python discovery problem, find VS indicates a Windows Visual Studio toolchain problem, and a compiler error indicates a failure in the native source itself or the installed SDK.

Run the checks for the current platform before reinstalling the package. A native dependency can fall back to compilation when no compatible pre-built binary exists, so the same package may work on one machine and fail on another.

node --version
npm --version
python3 --version
make --version
cc --version

# Windows PowerShell
py --version
where.exe cl

Install the compiler toolchain for your platform

On macOS, install Xcode Command Line Tools. On Unix-like systems, install a supported Python version, make, and a C/C++ compiler such as GCC or Clang.

On Windows, install Python and Visual Studio or Build Tools with the Desktop development with C++ workload. The old windows-build-tools npm package is not the current node-gyp setup path.

# macOS
xcode-select --install

# Ubuntu/Debian
sudo apt update
sudo apt install -y python3 make g++

# Windows PowerShell: list installed Python interpreters
py --list-paths

Match Python, node-gyp, and Node.js versions

Python 3.12 and newer requires node-gyp 10 or newer. If the package brings an older node-gyp through npm, upgrade the package or use a supported Python version instead of changing Python blindly.

Check the native package and its node-gyp dependency before choosing a Node.js version. Native addons may need a release that supports the ABI of the installed Node.js version.

npm ls node-gyp
npm ls <native-package-name>
node --version

# Show the package version that npm resolved
npm explain <native-package-name>

Reinstall only after the toolchain is ready

Once Python, the compiler, and the Node.js version are compatible, reinstall the dependencies. Keep package-lock.json when the project has one so that the retry uses the same dependency graph.

rm -rf node_modules

# Use the lockfile when it is committed
npm ci

# Otherwise resolve dependencies again
npm install

Tell node-gyp which Python to use

When several Python versions are installed, pass the executable explicitly to node-gyp. The npm_config_python environment variable is documented for npm-invoked builds, but direct --python is the least ambiguous option and avoids npm version-specific configuration behavior.

# Direct node-gyp invocation
node-gyp configure --python /path/to/python
node-gyp build

# npm invokes node-gyp
export npm_config_python=/path/to/python
npm install

# PowerShell
$Env:npm_config_python="C:\Path\python.exe"
npm install

A pre-built binary is unavailable

Many native packages try a pre-built binary first and compile from source when the binary does not match your operating system, CPU, or Node.js version. Upgrade the package to a release that supports your environment, or choose a compatible Node.js version.

npm outdated <native-package-name>
npm view <native-package-name> versions --json

Use --ignore-scripts only as a diagnostic

Skipping lifecycle scripts can confirm that the failure occurs during native compilation, but it leaves the addon unbuilt and may make the application unusable. It is not a production fix.

npm install --ignore-scripts

# Re-enable scripts after fixing the toolchain
npm rebuild