N-API Build Utilities
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
-
TypeError: napiBuildUtils.getNapiVersion is not a function
cause Incorrect CommonJS `require` syntax when expecting named exports or attempting to destructure a module that doesn't provide named exports via `module.exports = { ... }`. The primary export of `napi-build-utils` is an object containing functions.fixEnsure you are either using `const napiBuildUtils = require('napi-build-utils'); const napiVersion = napiBuildUtils.getNapiVersion();` or if using ESM or a compatible CJS setup, `const { getNapiVersion } = require('napi-build-utils');`. -
Error: N-API version X is not supported by current Node.js runtime.
cause This error likely originates from a native add-on's build or runtime process, informed by `napi-build-utils` checking the compatibility between the add-on's declared N-API versions (in `package.json`) and the current Node.js runtime's N-API support.fixVerify that the `binary.napi_versions` array in your `package.json` includes N-API versions compatible with your target Node.js environments. Update or rebuild your native add-on against a supported N-API version for the Node.js runtime in question. -
Cannot find module 'napi-build-utils'
cause The package `napi-build-utils` has not been installed or is not resolvable in the current project's `node_modules` directory.fixRun `npm install napi-build-utils` or `yarn add napi-build-utils` in your project directory to ensure the package is installed and accessible.
Warnings
- gotcha Version 2.0.0 introduced a SemVer major bump "out of an abundance of caution" to address a limitation when the N-API version reached 10 in Node.js v23.6.0. However, the API itself did not change. Users upgrading might expect breaking changes that are not present in the public API.
- gotcha Native add-ons built with N-API must explicitly declare their supported N-API versions within a `binary.napi_versions` property in their `package.json` file. Tools using `napi-build-utils` rely on this declaration for compatibility checks. Omitting or incorrectly configuring this property can lead to build failures or runtime issues for end-users.
- gotcha The `getNapiVersion()` function returns `undefined` if N-API is not supported by the currently running Node.js instance. This indicates an environment where N-API native add-ons cannot function, which is critical information for build tools.
Install
-
npm install napi-build-utils -
yarn add napi-build-utils -
pnpm add napi-build-utils
Imports
- napiBuildUtils
const napiBuildUtils = require('napi-build-utils');import napiBuildUtils from 'napi-build-utils';
- getNapiVersion
const { getNapiVersion } = require('napi-build-utils');import { getNapiVersion } from 'napi-build-utils'; - getNapiBuildVersions
const getNapiBuildVersions = require('napi-build-utils').getNapiBuildVersions;import { getNapiBuildVersions } from 'napi-build-utils'; - isSupportedVersion
import { isSupportedVersion } from 'napi-build-utils';
Quickstart
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.');