N-API Build Utilities

2.0.0 · active · verified Tue Apr 21

napi-build-utils is a pure JavaScript utility library designed specifically for developers creating tools that build Node-API native add-ons. It provides essential functions to programmatically determine the Node-API version supported by the current Node.js instance, read the declared supported N-API versions from a `package.json` file, and validate if a specific N-API version can be built in a given environment. Unlike the native add-ons it helps manage, this module itself is entirely written in JavaScript, ensuring broad compatibility without requiring compilation. The current stable version is 2.0.0, with releases typically occurring as new N-API versions emerge or specific Node.js runtime limitations need addressing. Its key differentiator is simplifying the often complex versioning and compatibility checks inherent in N-API development, preventing common build and runtime errors for native modules.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically retrieve the current Node.js N-API version, read declared N-API versions from a package.json, and check if a specific N-API version is compatible with both the runtime and the package's configuration. It simulates a package.json to illustrate the usage of `getNapiBuildVersions` and `isSupportedVersion`.

import { getNapiVersion, getNapiBuildVersions, isSupportedVersion } from 'napi-build-utils';
import { readFileSync } from 'fs';

// Simulate a package.json for demonstration
const packageJsonContent = {
  "name": "my-native-addon",
  "version": "1.0.0",
  "binary": {
    "napi_versions": [2, 3, 4, 5, 6, 7, 8, 9]
  }
};

// To use actual package.json, typically you would do:
// const pkg = JSON.parse(readFileSync('./package.json', 'utf8'));

console.log('--- N-API Build Utils Demo ---');

// 1. Get the N-API version supported by the current Node.js runtime
const currentNapiVersion = getNapiVersion();
if (currentNapiVersion) {
  console.log(`Current Node.js N-API Version: ${currentNapiVersion}`);
} else {
  console.log('N-API is not supported by the current Node.js instance.');
}

// 2. Get N-API versions declared in a package.json (simulated)
// getNapiBuildVersions expects a package.json object.
const declaredNapiVersions = getNapiBuildVersions(packageJsonContent);
console.log(`Declared N-API Versions in package.json: ${declaredNapiVersions.join(', ')}`);

// 3. Check if a specific N-API version is supported by the current environment and package
const targetVersion = 3;
const isTargetSupported = isSupportedVersion(targetVersion, currentNapiVersion, declaredNapiVersions);
console.log(`Is N-API v${targetVersion} supported by current environment and package? ${isTargetSupported}`);

const unsupportedVersion = 99;
const isUnsupported = isSupportedVersion(unsupportedVersion, currentNapiVersion, declaredNapiVersions);
console.log(`Is N-API v${unsupportedVersion} supported? ${isUnsupported}`);

console.log('\nThis script helps native add-on build tools verify N-API compatibility.');

view raw JSON →