How to fix Error: node-gyp build failed (native addon compilation error) in Node.js
This error occurs when Node.js fails to compile native C/C++ addons during package installation. It indicates a problem with the compilation environment, missing build tools, or incompatible dependencies required by node-gyp.
Quick fix
Fixes GYPERR
gcc --version
g++ --version
make --version- Verify your system has a C/C++ compiler installed
- Ensure Python is installed and accessible
- Clear npm cache and reinstall dependencies
- Specify Python version explicitly for node-gyp
gcc --versiong++ --versionmake --versionError: node-gyp build failed (native addon compilation error)
What this error means
The "node-gyp build failed" error occurs when Node.js attempts to compile native C/C++ addons (modules written in C or C++) and the compilation process fails. Node-gyp is a cross-platform command-line tool that builds native addons for Node.js packages. This error is particularly common when installing packages like `bcrypt`, `sqlite3`, `canvas`, `node-sass`, or other packages that include compiled native extensions. The build failure usually indicates missing dependencies, incompatible compiler versions, or misconfigured build environments. The error means your system lacks the necessary tools to compile C/C++ code, or the compilation environment is incompatible with the Node.js version you're using. This is a build-time issue, not a runtime issue, and occurs specifically during package installation when node-gyp attempts to compile source code.
Typical symptoms
- Installation fails with "node-gyp build failed" during npm install or yarn install
- Error messages reference missing compilers like "gcc", "clang", or "cl.exe"
- Build output shows errors like "command not found: make" or "python: command not found"
- Error mentions incompatible Python version or missing Python headers
- Windows users see "msbuild.exe not found" or missing Visual Studio build tools
- Installation works on some machines but fails on others
- Error appears specifically when installing packages with native extensions
- Verbose npm logs show compilation errors in C++ source files
Common causes
- Missing C/C++ compiler (gcc, clang, or MSVC) required to build native addons
- Python not installed or not in PATH (node-gyp requires Python 2 or 3)
- Missing build tools like make, cmake, or node-build-tools on Linux/macOS
- Visual Studio Build Tools not installed on Windows (required for native modules)
- Node.js and package are incompatible architectures (e.g., 32-bit Node.js with 64-bit package)
- Python version mismatch (some packages require specific Python versions)
- Outdated node-gyp version that doesn't support your Node.js version
- Missing system dependencies (headers, libraries) required by the native module
- Incorrect compiler configuration or incompatible compiler version
- Permissions issues preventing write access to build directories
How to fix "Error: node-gyp build failed (native addon compilation error)"
Check if your system has the necessary compiler tools. The specific tool depends on your operating system:
Linux (Ubuntu/Debian):
gcc --version
g++ --version
make --versionIf not installed, install build essentials:
sudo apt-get update
sudo apt-get install build-essential python3macOS:
clang --version
make --versionIf missing, install Xcode Command Line Tools:
xcode-select --installWindows:
Check if Visual Studio Build Tools are installed. You can verify by looking for the "cl.exe" compiler:
cl.exe /?If not installed, download Visual Studio Build Tools from the official Microsoft website and install the "Desktop development with C++" workload.
node-gyp requires Python to build native modules. Verify Python is installed and in your PATH:
python --versionOr for Python 3:
python3 --versionIf Python is not installed:
Linux (Ubuntu/Debian):
sudo apt-get install python3macOS:
brew install python3Windows:
Download Python from python.org and ensure you check "Add Python to PATH" during installation.
After installing, verify it's accessible:
python --version
which python # macOS/Linux
where python # WindowsSometimes cached build artifacts cause issues. Clear the cache and reinstall:
npm cache clean --force
rm -rf node_modules
rm package-lock.json
npm installFor yarn users:
yarn cache clean
rm -rf node_modules
rm yarn.lock
yarn installThis forces npm/yarn to download fresh packages and rebuild native modules from source.
If you have multiple Python versions installed, explicitly tell node-gyp which one to use:
npm install --python=/usr/bin/python3Or set it globally in your npm configuration:
npm config set python /usr/bin/python3For Windows with Python 3:
npm install --python=C:\Python39\python.exeVerify the configuration:
npm config get pythonSome packages like node-sass and bcrypt provide pre-built binaries. Force npm to use them instead of building from source:
npm install --ignore-scriptsAlternatively, use environment variables to specify architecture and skip building:
npm install --no-optionalFor packages with optional native dependencies, you can proceed without them:
npm install --legacy-peer-depsAfter installation, verify the package works without native bindings (functionality may be reduced but limited).
Older versions of node-gyp may not support your Node.js version. Update it globally:
npm install -g node-gyp@latestVerify the installation:
node-gyp --versionThen try installing your dependencies again:
npm installFor specific package issues, you can also update node-gyp within your project:
npm install node-gyp@latest --save-devEnsure your Node.js version and system architecture match the package requirements:
node --version
node -p "process.arch"
node -p "process.platform"Check if the package supports your Node.js version by looking at its package.json:
npm info package-nameIf you're on a newer Node.js version, try using an older LTS version:
nvm install 18 # or latest LTS
nvm use 18
npm installOn Windows, ensure you're not mixing 32-bit and 64-bit Node.js installations.
When all else fails, get more detailed output to understand exactly what's failing:
npm install --verboseOr with extra debugging:
npm install --loglevel=silly 2>&1 | tee build.logThis generates a detailed build log. Look for the actual C++ compiler error messages to understand what's missing:
npm install package-name --verbose 2>&1 | grep -A 10 "error:"Share these logs (with sensitive paths redacted) on GitHub issues or Stack Overflow to get community help with specific compiler errors.
Advanced notes
Alpine Linux Issues: Alpine Linux uses musl instead of glibc and doesn't include standard build tools by default. Install build-base, python3, and make to compile native modules.
Docker and Containers: In Docker, install build tools in your Dockerfile before running npm install. Use multi-stage builds to keep final image size small by removing build dependencies in a later stage.
CI/CD Pipelines: GitHub Actions, GitLab CI, and other CI systems may not have build tools pre-installed. Add explicit installation steps before npm install. Use container images like node:18-alpine with pre-installed build tools.
Prebuilt Binaries: Packages like bcrypt, sqlite3, and canvas support prebuilt binaries via prebuild. These significantly speed up installation and reduce build failures. Check if your package uses prebuild before forcing source compilation.
Native Module Caching: In development, once a package is compiled, node-gyp caches it. Clearing node_modules usually forces a rebuild, but you can also manually delete the .node files to force recompilation.
Python 2 vs Python 3: Older packages may require Python 2 (which is EOL). If a package requires Python 2, consider upgrading it or using a maintained alternative with Python 3 support.
macOS Monterey and Later: Recent macOS versions have stricter permissions. You may need sudo for some build operations, though this is generally not recommended. Prefer updating node-gyp and packages instead.
Performance on Slow Systems: Compilation on low-end hardware can timeout. Increase the timeout: npm install --verbose --timeout=120000 (milliseconds).
Cross-Compilation: Building for different architectures (e.g., ARM on x86 or vice versa) requires cross-compilation tools. Use node-gyp with --target and --arch flags to specify target platform explicitly.
Frequently asked questions
- What does "GYPERR" mean?
- js attempts to compile native C/C++ addons (modules written in C or C++) and the compilation process fails. js packages. This error is particularly common when installing packages like bcrypt, sqlite3, canvas, node-sass, or other packages that include compiled native extensions.
- How do I fix "GYPERR"?
- This error occurs when Node.js fails to compile native C/C++ addons during package installation. It indicates a problem with the compilation environment, missing build tools, or incompatible dependencies required by node-gyp.
- What causes "GYPERR"?
- Missing C/C++ compiler (gcc, clang, or MSVC) required to build native addons Python not installed or not in PATH (node-gyp requires Python 2 or 3) Missing build tools like make, cmake, or node-build-tools on Linux/macOS
- Is the "GYPERR" 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
- ๐Node.js Official Documentation
- ๐node-gyp Official Repository(Official Docs)
- ๐Node.js Package Manager Documentation(Official Docs)
- ๐node-gyp GitHub Issues(GitHub)
- ๐Windows build tools setup guide(GitHub)