Node.js Native Addon Prebuild Loader

5.2.2 · active · verified Sun Apr 19

node-gyp-build is a critical utility for Node.js native addon developers, streamlining the distribution and loading of C/C++ bindings. It acts as both an `npm install` script and a runtime loader, prioritizing the use of precompiled binaries (prebuilds) over source compilation. Currently at stable version 5.2.2, its release cadence is typically tied to major Node.js ABI changes or significant updates in the native module tooling ecosystem. Its key differentiator is its seamless integration with `prebuildify` to provide robust cross-platform support, including different Node.js versions, Electron environments, and various architectures (e.g., `musl` for Alpine Linux, specific ARM versions). This significantly reduces installation friction for end-users, who often lack the necessary C++ toolchains. It intelligently detects runtime specifics like `libc` and `armv` to load the most appropriate prebuild, with options for users to override and force source compilation via `npm install --build-from-source`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure `node-gyp-build` as an `npm install` script and use it to load a native module's binding at runtime, favoring prebuilds.

// package.json (excerpt)
{
  "name": "my-native-addon",
  "version": "1.0.0",
  "description": "Demonstrates node-gyp-build usage for native addons",
  "main": "index.js",
  "scripts": {
    "install": "node-gyp-build", // This script runs during `npm install`
    "test": "node index.js"
    // For generating prebuilds, you'd typically have: 
    // "prebuild": "prebuildify -t napi -t armv -t libc"
  },
  "dependencies": {
    "node-gyp-build": "^5.0.0" // Dependency for the loader and install script
  },
  "devDependencies": {
    "node-gyp": "^10.0.0", // Required for compiling from source
    "prebuildify": "^5.0.0" // Required for generating prebuilds
  },
  "gypfile": true // Indicates a binding.gyp file exists
}

// index.js (in the root of your native addon package)
const path = require('path');
const loadNativeBinding = require('node-gyp-build');

let binding;
try {
  // `loadNativeBinding(__dirname)` will search for prebuilds in `__dirname/prebuilds/...`
  // and then `__dirname/build/Release` if no prebuilds are found or forced.
  binding = loadNativeBinding(__dirname);
  console.log("Native addon loaded successfully!");

  // Assuming your native addon exposes a simple function, e.g., 'addNumbers'
  // This part requires an actual native addon to exist and be built.
  if (typeof binding.addNumbers === 'function') {
    const result = binding.addNumbers(5, 7);
    console.log(`Result from native addon: 5 + 7 = ${result}`);
  } else {
    console.warn("Native addon function 'addNumbers' not found or is a placeholder.");
  }
} catch (error) {
  console.error("Failed to load native addon:", error.message);
  console.warn("Ensure prebuilds are generated (e.g., with 'prebuildify') or build tools for 'node-gyp' are installed.");
  console.warn("To force compilation from source, try: npm install --build-from-source");
}

view raw JSON →