| name | node-gyp |
| description | Build and troubleshoot native Node.js addons using node-gyp. Use when working with native addon compilation, binding.gyp configuration, NODE_MODULE_VERSION mismatch errors, Electron native module rebuilds, or packages like bcrypt, node-sass, sqlite3, sharp. Covers platform-specific build setup (Linux/macOS/Windows), N-API configuration, cross-compilation, and pre-built binary distribution. |
node-gyp: Native Node.js Addon Building
node-gyp compiles C/C++ code into native Node.js modules (.node files). It uses GYP configuration to create platform-specific build files.
Build Pipeline: binding.gyp ā configure ā platform build files ā compile ā .node binary
Key Concepts
Native Addons
Binary modules providing Node.js bindings to C/C++ libraries. Compiled for specific:
- Node.js versions (each has a MODULE_VERSION number)
- Platforms (Linux, macOS, Windows)
- Architectures (x64, arm64, ia32)
binding.gyp
Configuration file defining how to build your addon. JSON-like syntax with GYP features.
N-API (Node-API)
ABI-stable API for native addons. Modules work across Node.js versions without recompiling. Always use for new projects.
Variable Expansion
<(var) - String expansion
<@(var) - List expansion
<!(cmd) - Command output as string
<!@(cmd) - Command output as list
Platform Setup
Linux:
sudo apt-get install build-essential python3
macOS:
xcode-select --install
Windows (choose one):
choco install python visualstudio2022-workload-vctools -y
npm install --global --production windows-build-tools
Essential Commands
node-gyp rebuild
node-gyp clean
node-gyp configure
node-gyp build
node-gyp rebuild -j max
node-gyp rebuild --debug
node-gyp install
When to use each:
rebuild - Default choice, ensures clean build
configure then build - When iterating on binding.gyp
clean - When switching Node versions or architectures
install - When headers are missing or corrupted
Basic binding.gyp
{
"targets": [{
"target_name": "addon",
"sources": ["src/addon.cc"],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"
],
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"],
"cflags!": ["-fno-exceptions"],
"cflags_cc!": ["-fno-exceptions"]
}]
}
Key fields:
target_name - Output filename (addon.node)
sources - C/C++ source files
include_dirs - Header search paths
defines - Preprocessor definitions
cflags! / cflags_cc! - Remove default flags (! means remove)
N-API Setup (Recommended)
package.json:
{
"dependencies": {
"node-addon-api": "^7.0.0"
},
"scripts": {
"install": "node-gyp rebuild"
}
}
binding.gyp:
{
"targets": [{
"target_name": "addon",
"sources": ["src/addon.cc"],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"
],
"dependencies": [
"<!(node -p \"require('node-addon-api').gyp\")"
],
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"]
}]
}
Quick Troubleshooting Decision Tree
Build failed?
ā
āā Python error?
ā āā npm config set python /usr/bin/python3
ā
āā Visual Studio error? (Windows)
ā āā node-gyp rebuild --msvs_version=2022
ā
āā NODE_MODULE_VERSION mismatch?
ā āā npm rebuild
ā
āā Architecture mismatch? (M1/M2 Mac)
ā āā rm -rf node_modules && npm install
ā
āā Missing headers?
ā āā node-gyp install
ā
āā Compilation/linking error?
ā āā Check binding.gyp libraries, run with --verbose
ā
āā Still failing?
āā See references/troubleshooting.md for detailed solutions
References
For detailed guidance on specific topics: