Skip to content
napi-bindings

Search

Search pages, projects and the Node-API reference

Browse Node-APIshow
Node-API

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. 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. 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. 3

    Initialization

    The runtime calls your init function with a napi_env and the exports object; you attach functions and classes.

  4. 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

js_native_api.h
The ECMAScript surface: values, objects, functions, errors, lifetimes, promises. Engine independent and implementable by any runtime. 149 entries
node_api.h
Node.js-specific parts: module registration, Buffer, async work, libuv, thread-safe functions, cleanup hooks. Includes js_native_api.h. 40 entries

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

Read the guide

The guide follows the official Node.js documentation, section by section, synchronised at every build.