Node-API Headers
The `node-api-headers` package provides the essential C/C++ header files required for building Node.js native add-ons using the Node-API (formerly N-API). These headers enable developers to create native modules that benefit from ABI stability across different Node.js versions, simplifying maintenance and ensuring forward compatibility. The package, currently at version 1.8.0, maintains a frequent release cadence, often monthly or bi-monthly, specifically tracking and incorporating updates from the Node.js core repository. This package is crucial for build tools like `node-gyp` and `CMake.js`, which rely on these headers to compile native C/C++ code into `.node` files runnable within Node.js applications. It differentiates itself by being the official source for these headers, abstracting away the need to download full Node.js source distributions for native module development.
Common errors
-
Module not found: Can't resolve 'node-api-headers'
cause Attempting to `import` or `require` `node-api-headers` as if it were a JavaScript module.fixThis package is not a JavaScript module. It provides C/C++ headers for native add-on compilation. It should not be imported directly into JS/TS application code. -
error: 'NAPI_VERSION' was not declared in this scope
cause Native add-on compilation failed because the Node-API headers were not correctly included or found by the build system.fixEnsure your `binding.gyp` (or equivalent build configuration) correctly specifies the `include_dirs` to point to `require('node-api-headers').include_dir`. Verify `node-gyp` is installed and configured correctly. For CMake.js, ensure your `CMakeLists.txt` is set up to find the Node-API headers. -
Error: The module '\path\to\your\addon.node' was compiled against a different Node.js version using NODE_MODULE_VERSION NNN. This version of Node.js requires NODE_MODULE_VERSION MMM. Please try re-compiling or re-installing the module (for instance, using `npm rebuild`).
cause The native add-on was compiled against Node-API headers for a different Node.js ABI version than the one currently running the application.fixThe Node-API aims for ABI stability, but issues can still arise if toolchains are mismatched or `node-gyp` picks up incorrect headers. Run `npm rebuild` to recompile all native add-ons against the currently installed Node.js version. Ensure `node-api-headers` is updated to a compatible version.
Warnings
- gotcha The `node-api-headers` package is not a JavaScript module and does not export any JavaScript functions or objects for direct `import` or `require` in application code. It exclusively provides C/C++ header files.
- gotcha Package versions of `node-api-headers` are decoupled from Node.js runtime versions. While new releases often align with Node.js core updates, the package's version (e.g., 1.x.x) does not directly correspond to Node.js major versions (e.g., v20, v21). Always ensure the Node-API C ABI version used in your native add-on is compatible with your target Node.js runtime.
- gotcha When building native add-ons, including extraneous Node.js C++ headers (e.g., `<node.h>`, `<v8.h>`) in addition to Node-API headers (`<node_api.h>`) can break ABI stability. Node-API provides stability, but other internal Node.js C++ APIs do not guarantee compatibility across major Node.js versions.
Install
-
npm install node-api-headers -
yarn add node-api-headers -
pnpm add node-api-headers
Imports
- node-api-headers
import * as napiHeaders from 'node-api-headers'
N/A (not a JavaScript module)
- NAPI_VERSION
import { NAPI_VERSION } from 'node-api-headers'#include <node_api.h> (in C/C++)
- include_dir
import { include_dir } from 'node-api-headers'require('node-api-headers').include_dir
Quickstart
/* file: myaddon.cc */
#include <node_api.h>
napi_value Method(napi_env env, napi_callback_info info) {
napi_value greeting;
napi_status status;
status = napi_create_string_utf8(env, "Hello from Node-API C++ addon!", NAPI_AUTO_LENGTH, &greeting);
if (status != napi_ok) return nullptr;
return greeting;
}
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor desc = {
"hello", 0, Method, 0, 0, 0, napi_default, 0
};
status = napi_define_properties(env, exports, 1, &desc);
if (status != napi_ok) return nullptr;
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
/* file: binding.gyp */
{
"targets": [
{
"target_name": "myaddon",
"sources": [ "myaddon.cc" ],
"include_dirs": [
"<!@(node -p \"require('node-api-headers').include_dir\")"
]
}
]
}
/* file: index.js */
const addon = require('bindings')('myaddon');
console.log(addon.hello());