node-addon-api

NOTE: The default branch has been renamed! master is now named main

If you have a local clone, you can update it by running:

git branch -m master main
git fetch origin
git branch -u origin/main main

node-addon-api module

This module contains header-only C++ wrapper classes which simplify the use of the C based Node-APIarrow-up-right provided by Node.js when using C++. It provides a C++ object model and exception handling semantics with low overhead.

There are three options for implementing addons: Node-API, nan, or direct use of internal V8, libuv, and Node.js libraries. Unless there is a need for direct access to functionality that is not exposed by Node-API as outlined in C/C++ addonsarrow-up-right in Node.js core, use Node-API. Refer to C/C++ addons with Node-APIarrow-up-right for more information on Node-API.

Node-API is an ABI stable C interface provided by Node.js for building native addons. It is independent of the underlying JavaScript runtime (e.g. V8 or ChakraCore) and is maintained as part of Node.js itself. It is intended to insulate native addons from changes in the underlying JavaScript engine and allow modules compiled for one version to run on later versions of Node.js without recompilation.

The node-addon-api module, which is not part of Node.js, preserves the benefits of the Node-API as it consists only of inline code that depends only on the stable API provided by Node-API. As such, modules built against one version of Node.js using node-addon-api should run without having to be rebuilt with newer versions of Node.js.

It is important to remember that other Node.js interfaces such as libuv (included in a project via #include <uv.h>) are not ABI-stable across Node.js major versions. Thus, an addon must use Node-API and/or node-addon-api exclusively and build against a version of Node.js that includes an implementation of Node-API (meaning an active LTS version of Node.js) in order to benefit from ABI stability across Node.js major versions. Node.js provides an ABI stability guidearrow-up-right containing a detailed explanation of ABI stability in general, and the Node-API ABI stability guarantee in particular.

As new APIs are added to Node-API, node-addon-api must be updated to provide wrappers for those new APIs. For this reason, node-addon-api provides methods that allow callers to obtain the underlying Node-API handles so direct calls to Node-API and the use of the objects/methods provided by node-addon-api can be used together. For example, in order to be able to use an API for which the node-addon-api does not yet provide a wrapper.

APIs exposed by node-addon-api are generally used to create and manipulate JavaScript values. Concepts and operations generally map to ideas specified in the ECMA262 Language Specification.

The Node-API Resourcearrow-up-right offers an excellent orientation and tips for developers just getting started with Node-API and node-addon-api.

Current version: 5.1.0

(See CHANGELOG.mdarrow-up-right for complete Changelog)

NPMarrow-up-right NPMarrow-up-right

node-addon-api is based on Node-APIarrow-up-right and supports using different Node-API versions. This allows addons built with it to run with Node.js versions which support the targeted Node-API version. However the node-addon-api support model is to support only the active LTS Node.js versions. This means that every year there will be a new major which drops support for the Node.js LTS version which has gone out of service.

The oldest Node.js version supported by the current version of node-addon-api is Node.js 14.x.

Setup

API Documentation

The following is the documentation for node-addon-api.

Examples

Are you new to node-addon-api? Take a look at our examplesarrow-up-right

Tests

To run the node-addon-api tests do:

To avoid testing the deprecated portions of the API run

To run the tests targeting a specific version of Node-API run

where X is the version of Node-API you want to target.

To run a specific unit test, filter conditions are available

Example: compile and run only tests on objectwrap.cc and objectwrap.js

Multiple unit tests cane be selected with wildcards

Example: compile and run all test files ending with "reference" -> function_reference.cc, object_reference.cc, reference.cc

Multiple filter conditions can be joined to broaden the test selection

Example: compile and run all tests under folders threadsafe_function and typed_threadsafe_function and also the objectwrap.cc file npm run unit --filter='*function objectwrap'

Debug

To run the node-addon-api tests with --debug option:

If you want a faster build, you might use the following option:

Take a look and get inspired by our test suitearrow-up-right

Benchmarks

You can run the available benchmarks using the following command:

See benchmark/README.mdarrow-up-right for more details about running and adding benchmarks.

More resource and info about native Addons

As node-addon-api's core mission is to expose the plain C Node-API as C++ wrappers, tools that facilitate n-api/node-addon-api providing more convenient patterns for developing a Node.js add-on with n-api/node-addon-api can be published to NPM as standalone packages. It is also recommended to tag such packages with node-addon-api to provide more visibility to the community.

Quick links to NPM searches: keywords:node-addon-apiarrow-up-right.

Other bindings

Badges

The use of badges is recommended to indicate the minimum version of Node-API required for the module. This helps to determine which Node.js major versions are supported. Addon maintainers can consult the Node-API support matrixarrow-up-right to determine which Node.js versions provide a given Node-API version. The following badges are available:

Node-API v1 Badge
Node-API v2 Badge
Node-API v3 Badge
Node-API v4 Badge
Node-API v5 Badge
Node-API v6 Badge
Node-API v7 Badge
Node-API v8 Badge
Node-API Experimental Version Badge

Contributing

We love contributions from the community to node-addon-api! See CONTRIBUTING.mdarrow-up-right for more details on our philosophy around extending this module.

Team members

Active

Name
GitHub Link

Emeritus

Licensed under MIT

Last updated