{"library":"napi-build-utils","title":"N-API Build Utilities","description":"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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install napi-build-utils"],"cli":null},"imports":["import napiBuildUtils from 'napi-build-utils';","import { getNapiVersion } from 'napi-build-utils';","import { getNapiBuildVersions } from 'napi-build-utils';","import { isSupportedVersion } from 'napi-build-utils';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { getNapiVersion, getNapiBuildVersions, isSupportedVersion } from 'napi-build-utils';\nimport { readFileSync } from 'fs';\n\n// Simulate a package.json for demonstration\nconst packageJsonContent = {\n  \"name\": \"my-native-addon\",\n  \"version\": \"1.0.0\",\n  \"binary\": {\n    \"napi_versions\": [2, 3, 4, 5, 6, 7, 8, 9]\n  }\n};\n\n// To use actual package.json, typically you would do:\n// const pkg = JSON.parse(readFileSync('./package.json', 'utf8'));\n\nconsole.log('--- N-API Build Utils Demo ---');\n\n// 1. Get the N-API version supported by the current Node.js runtime\nconst currentNapiVersion = getNapiVersion();\nif (currentNapiVersion) {\n  console.log(`Current Node.js N-API Version: ${currentNapiVersion}`);\n} else {\n  console.log('N-API is not supported by the current Node.js instance.');\n}\n\n// 2. Get N-API versions declared in a package.json (simulated)\n// getNapiBuildVersions expects a package.json object.\nconst declaredNapiVersions = getNapiBuildVersions(packageJsonContent);\nconsole.log(`Declared N-API Versions in package.json: ${declaredNapiVersions.join(', ')}`);\n\n// 3. Check if a specific N-API version is supported by the current environment and package\nconst targetVersion = 3;\nconst isTargetSupported = isSupportedVersion(targetVersion, currentNapiVersion, declaredNapiVersions);\nconsole.log(`Is N-API v${targetVersion} supported by current environment and package? ${isTargetSupported}`);\n\nconst unsupportedVersion = 99;\nconst isUnsupported = isSupportedVersion(unsupportedVersion, currentNapiVersion, declaredNapiVersions);\nconsole.log(`Is N-API v${unsupportedVersion} supported? ${isUnsupported}`);\n\nconsole.log('\\nThis script helps native add-on build tools verify N-API compatibility.');","lang":"javascript","description":"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`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}