{"id":11411,"library":"node-api-version","title":"Node.js and Electron N-API Version Checker","description":"The `node-api-version` package provides a programmatic way to determine the maximum Node-API (N-API) version supported by specific Node.js or Electron runtime versions. This utility is critical for developers working with native Node.js addons, enabling them to ensure compatibility and manage prebuilt binaries efficiently across different runtime environments. By querying N-API support directly, it helps avoid ABI mismatches and streamlines the native module compilation and distribution process. The current stable version is 0.2.1, indicating a stable but early-stage utility, likely with an infrequent release cadence tied to updates in Node.js and Electron. Its primary differentiator is offering a simple, automated lookup without requiring manual maintenance of N-API version tables.","status":"active","version":"0.2.1","language":"javascript","source_language":"en","source_url":"https://github.com/timfish/node-api-version","tags":["javascript","typescript"],"install":[{"cmd":"npm install node-api-version","lang":"bash","label":"npm"},{"cmd":"yarn add node-api-version","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-api-version","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For CommonJS, use `const { fromNodeVersion } = require('node-api-version');`. Package ships TypeScript types.","wrong":"const fromNodeVersion = require('node-api-version').fromNodeVersion;","symbol":"fromNodeVersion","correct":"import { fromNodeVersion } from 'node-api-version';"},{"note":"This function specifically targets Electron versions. Ensure correct import syntax for your module system (ESM or CommonJS).","wrong":"const fromElectronVersion = require('node-api-version').fromElectronVersion;","symbol":"fromElectronVersion","correct":"import { fromElectronVersion } from 'node-api-version';"},{"note":"The package exports named functions, not a default export. Use destructuring for CommonJS and named imports for ESM.","wrong":"import nodeApiVersion from 'node-api-version';","symbol":"All functions (CJS)","correct":"const { fromNodeVersion, fromElectronVersion } = require('node-api-version');"}],"quickstart":{"code":"import { fromNodeVersion, fromElectronVersion } from 'node-api-version';\n\n// Determine the Node-API version for specific Node.js releases\nconst napiNode9 = fromNodeVersion('9.0.0');\nconsole.log(`Node 9.0.0 N-API version: ${napiNode9 || 'N/A'}`);\n\nconst napiNode12 = fromNodeVersion('12.13.0');\nconsole.log(`Node 12.13.0 N-API version: ${napiNode12 || 'N/A'}`);\n\nconst napiNode16 = fromNodeVersion('16.0.0');\nconsole.log(`Node 16.0.0 N-API version: ${napiNode16 || 'N/A'}`);\n\n// Determine the Node-API version for specific Electron releases\nconst napiElectron2 = fromElectronVersion('2.0.0');\nconsole.log(`Electron 2.0.0 N-API version: ${napiElectron2 || 'N/A'}`);\n\nconst napiElectron13 = fromElectronVersion('13.0.0');\nconsole.log(`Electron 13.0.0 N-API version: ${napiElectron13 || 'N/A'}`);\n\nconst napiElectron15Nightly = fromElectronVersion('15.0.0-nightly.20210629');\nconsole.log(`Electron 15.0.0-nightly N-API version: ${napiElectron15Nightly || 'N/A'}`);\n\n// Example of an unsupported version\nconst napiUnknown = fromNodeVersion('0.10.0');\nconsole.log(`Node 0.10.0 N-API version: ${napiUnknown || 'N/A'}`);\n","lang":"typescript","description":"This quickstart demonstrates how to use `fromNodeVersion` and `fromElectronVersion` to query Node-API compatibility for various runtime versions, showing results for both supported and unsupported inputs."},"warnings":[{"fix":"Implement explicit checks for `undefined` results: `const napiVersion = fromNodeVersion('...'); if (napiVersion === undefined) { /* handle unsupported */ }`","message":"The functions `fromNodeVersion` and `fromElectronVersion` return `undefined` when the provided version does not support Node-API or when the version is unknown/unsupported by the library's internal mapping. Always handle `undefined` results.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Pin to exact versions for production environments (`\"node-api-version\": \"0.2.1\"`) and manually verify functionality after any upgrade, even for minor version bumps.","message":"As a pre-1.0.0 package (current version 0.2.1), `node-api-version` follows semantic versioning but with the understanding that minor versions *can* introduce breaking changes. Developers should review release notes carefully before upgrading minor versions.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"For pure Node.js ESM projects, ensure your `package.json` has `\"type\": \"module\"` and consider using bundlers or transpilers for consistency. Otherwise, stick to `require()` for CommonJS contexts.","message":"While the package ships TypeScript types, its `package.json` primarily specifies a CommonJS `main` entry point. While modern bundlers and TypeScript can usually resolve ESM `import` statements, direct Node.js execution might require specific ESM configuration or the use of `require()` for older Node.js versions.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use `const { fromNodeVersion } = require('node-api-version');` for CommonJS.","cause":"Incorrect CommonJS import syntax, attempting to call a function directly from `require('pkg')` instead of destructuring.","error":"TypeError: fromNodeVersion is not a function"},{"fix":"Verify the version string for typos and ensure it's a version that is expected to have N-API support. Handle the `undefined` return explicitly in your code.","cause":"The input Node.js or Electron version is either too old to support Node-API or is not recognized/mapped by the library.","error":"fromNodeVersion(...) returns undefined"},{"fix":"Use named imports: `import { fromNodeVersion, fromElectronVersion } from 'node-api-version';`","cause":"Attempting to use a default import (`import nodeApiVersion from 'node-api-version';`) when the package only provides named exports.","error":"TS2305: Module '\"node-api-version\"' has no exported member 'default'."}],"ecosystem":"npm"}