Writing addons in various programming languages
Source: Node.js documentationNode-API is a C API that ensures ABI stability across Node.js versions and different compiler levels. With this stability guarantee, it is possible to write addons in other programming languages on top of Node-API. Refer to language and engine bindings for more programming languages and engines support details.
node-addon-api is the official C++ binding that provides a more efficient way to
write C++ code that calls Node-API. This wrapper is a header-only library that offers an inlinable C++ API.
Binaries built with node-addon-api will depend on the symbols of the Node-API
C-based functions exported by Node.js. The following code snippet is an example
of node-addon-api:
Object obj = Object::New(env);
obj["foo"] = String::New(env, "bar");
The above node-addon-api C++ code is equivalent to the following C-based
Node-API code:
napi_status status;
napi_value object, string;
status = napi_create_object(env, &object);
if (status != napi_ok) {
napi_throw_error(env, ...);
return;
}
status = napi_create_string_utf8(env, "bar", NAPI_AUTO_LENGTH, &string);
if (status != napi_ok) {
napi_throw_error(env, ...);
return;
}
status = napi_set_named_property(env, object, "foo", string);
if (status != napi_ok) {
napi_throw_error(env, ...);
return;
}
The end result is that the addon only uses the exported C APIs. Even though the addon is written in C++, it still gets the benefits of the ABI stability provided by the C Node-API.
When using node-addon-api instead of the C APIs, start with the API docs
for node-addon-api.
The Node-API Resource offers
an excellent orientation and tips for developers just getting started with
Node-API and node-addon-api. Additional media resources can be found on the
Node-API Media page.