Node-GYP Build (Prebuild-aware Native Addon Loader)

4.8.4 · active · verified Sun Apr 19

node-gyp-build is a critical utility for distributing Node.js native addons, acting as a wrapper around node-gyp to support prebuilt binaries. Its primary function is to check for and load prebuilt native modules, or fall back to compiling from source using node-gyp if no suitable prebuild is found. This significantly reduces installation times and improves cross-platform compatibility by avoiding local compilation for many users. It works in tandem with 'prebuildify' to generate and bundle these prebuilds, handling various target environments including Node.js and Electron, and accommodating different libc (e.g., glibc, musl) and ARM architectures. The current stable version is 4.8.4, with releases generally following the node-gyp and prebuildify ecosystem, focusing on stability and compatibility with new Node.js ABIs and platforms. Key differentiators include its robust prebuild discovery mechanism, seamless integration into npm install scripts, and support for complex prebuild tagging.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to configure node-gyp-build as an npm 'install' script within `package.json` and use its loader function in `index.js` to load a native addon. This setup prioritizes prebuilt binaries and provides a fallback for local compilation.

// package.json - configure the 'install' script and dependencies
{
  "name": "my-native-hello-world",
  "version": "1.0.0",
  "description": "A sample native module using node-gyp-build and prebuildify",
  "main": "index.js",
  "scripts": {
    "install": "node-gyp-build",
    "prebuild": "prebuildify --strip", // Requires 'prebuildify' to be installed locally
    "postinstall": "node index.js" // To test if the binding loads after install
  },
  "dependencies": {
    "node-gyp-build": "^4.0.0"
  }
}

// index.js - load the native binding
const path = require('path');

console.log("Attempting to load native binding...");
let helloBinding;
try {
  helloBinding = require('node-gyp-build')(__dirname);
  console.log("Native binding loaded successfully!");

  // Assuming the native addon exports a 'hello' function
  if (typeof helloBinding.hello === 'function') {
    const message = helloBinding.hello();
    console.log(`Native greeting: ${message}`); // Expected: 'Hello from native!'
  } else {
    console.warn("Native binding does not export a 'hello' function.");
  }
} catch (e) {
  console.error(`Failed to load native binding: ${e.message}`);
  console.warn("Consider running 'npm install --build-from-source' if prebuilds are missing or incompatible.");
  // You might want to provide a fallback or re-throw based on your application's needs
}

/*
  // Example of a minimal C++ source file for 'hello.cc' (requires a binding.gyp)
  // Save this as hello.cc in your project root.

  #include <napi.h>

  Napi::String Method(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();
    return Napi::String::New(env, "Hello from native!");
  }

  Napi::Object Init(Napi::Env env, Napi::Object exports) {
    exports.Set(Napi::String::New(env, "hello"), Napi::Function::New(env, Method));
    return exports;
  }

  NODE_API_MODULE(hello, Init)

  // Example binding.gyp for 'hello.cc'
  // Save this as binding.gyp in your project root.

  {
    "targets": [
      {
        "target_name": "hello",
        "sources": [ "hello.cc" ]
      }
    ]
  }
*/

view raw JSON →