Node-API in a nutshell
Node-API (formerly N-API) is an ABI-stable C interface for building native addons. It is part of Node.js core and independent from the JavaScript engine underneath.
Why it exists
Before Node-API, native addons were written against V8 and the internals of Node.js (often through NAN). Every major release could change those APIs, so addons had to be recompiled, and often rewritten, for each version of Node.js.
Node-API puts a stable C boundary between addons and the runtime. Addons compiled against one Node-API version keep working on every later version of Node.js, and because the interface never mentions V8, other runtimes can implement it too. It has been stable since Node.js 8.12.0 and is the recommended way to write addons.
How an addon is loaded
- 1
Compile a shared library
The addon is a .node file: a shared library that includes node_api.h and leaves the napi_* symbols undefined.
- 2
The runtime loads it
require() / process.dlopen() maps the library and looks for the registration done by NAPI_MODULE or NAPI_MODULE_INIT.
- 3
Initialization
The runtime calls your init function with a napi_env and the exports object; you attach functions and classes.
- 4
Calls resolve to the host
Every napi_* call is served by the process that loaded the addon: node, Electron, Bun, Deno or quickjs-napi.
Linking with undefined symbols is the part toolchains struggle with: Windows needs an import library and macOS refuses undefined symbols by default. node-api-stub and Marmotta solve it for CMake and zero-config builds.
Two headers, two scopes
Choosing a Node-API version
Define NAPI_VERSION before including the header to target a version; without it, Node.js builds for version 8. Since version 9, runtimes support every version between 8 and the highest they implement, so opting into a newer version is explicit. The latest is Node-API 10.
#define NAPI_VERSION 10
#include <node_api.h>Core concepts
node-addon-api wraps Node-API in header-only C++ classes, napi-rs and neon do the same for Rust, and napi-go for Go. They all compile to the same ABI-stable calls. See the ecosystem.
Read the guide
The guide follows the official Node.js documentation, section by section, synchronised at every build.
- Introduction
- Writing addons in various programming languages
- Implications of ABI stability
- Building
- Usage
- Node-API version matrix
- Environment life cycle APIs2 APIs
- Basic Node-API data types22 APIs
- Error handling15 APIs
- Object lifetime management14 APIs
- Module registration
- Working with JavaScript values60 APIs
- Working with JavaScript values and abstract operations18 APIs
- Working with JavaScript properties20 APIs
- Working with JavaScript functions5 APIs
- Object wrap8 APIs
- Simple asynchronous operations4 APIs
- Custom asynchronous operations5 APIs
- Version management2 APIs
- Memory management1 APIs
- Promises4 APIs
- Script execution1 APIs
- libuv event loop1 APIs
- Asynchronous thread-safe function calls7 APIs
- Miscellaneous utilities1 APIs