This article is excerpted from Nodejs Study Notes. For more chapters and updates, visit github.

N – API description

Node.js 8.0 was released in June 2017, and among the updated features, n-API was included. For those of you who have written or used the Node extension, there are many cases where the node extension fails to compile when upgrading the node version. Because node extensions rely heavily on the API exposed by V8, and different versions of Node may depend on different VERSIONS of V8, once the node version is upgraded, the previously healthy Node extension will fail to compile.

This situation is definitely bad for the Node ecosystem, and the introduction of N-API is an attempt to improve this situation. It has nothing to do with the underlying JS engine, and as long as the N-API exposed apis are stable enough, the authors of Node extensions don’t have to worry too much about node upgrades.

How to use n-API

It is important to note that n-API is not a replacement for the original implementation of Node extensions. It simply provides a set of low-level, unrelated apis to help developers write cross-version Node extensions. How to write, compile, and use the extension is pretty much the same as before.

This article will take you through the steps of preparing the environment, writing the extension, compiling, and running the N-API from a super simple example.

Note: The N-API is still in the experimental stage, and the examples provided in the official documentation are all problematic, so be careful if used in a production environment.

1. Environment preparation

First, n-API was introduced with version 8.0, so make sure you have version 8.0 installed locally. The author is using NVM, the reader can choose the installation method.

NVM I 8.0 NVM Use 8.0Copy the code

Then, install Node-gyp, which will be used to compile the extension.

npm install -g node-gyp
Copy the code

Create the project directory and initialize package.json.

mkdir hello & cd hello The directory name is optional
npm init -f
Copy the code

2. Write extensions

Create hello.cc as the source file for the extension.

mkdir src
touch src/hello.cc
Copy the code

Edit hello.cc and enter the following.

#include <node_api.h>

// The actual exposed method, here simply returns a string
napi_value HelloMethod (napi_env env, napi_callback_info info) {
    napi_value world;
    napi_create_string_utf8(env, "world".5, &world);
    return world;
}

// Extended initialization method, where
// env: environment variable
// exports, module: node module exposed objects
void Init (napi_env env, napi_value exports, napi_value module.void* priv) {
    // Napi_property_descriptor is a structure that describes the description of the property/method exposed by the extension
    napi_property_descriptor desc = { "hello".0, HelloMethod, 0.0.0, napi_default, 0 };
    napi_define_properties(env, exports, 1, &desc);  // Define the exposed method
}

NAPI_MODULE(hello, Init);  // Register the extension. The extension name is hello. Init is the initialization method for the extension
Copy the code

3. Compile the extension

First, create the compile description file binding.gyp.

{
  "targets": [{"target_name": "hello"."sources": [ "./src/hello.cc"]]}}Copy the code

Then, run the following command to compile.

node-gyp rebuild
Copy the code

4. Call the extension

Bindings is installed before it is convenient to call the extension.

npm install --save bindings
Copy the code

Then, create app.js and invoke the extension you just compiled.

var addon = require('bindings') ('hello');

console.log( addon.hello() );  // world
Copy the code

Run the code, and since n-API is currently Experimental, add — API -modules.

node --napi-modules app.js
Copy the code

The output is as follows

{"path":"/data/github/abi-stable-node-addon-examples/1_hello_world/napi/build/Release/hello.node"}
world
(node:6500) Warning: N-API is an experimental feature and could change at any time.
Copy the code

A link to the

N-API:nodejs.org/api/n-api.h…

C + + Addons:nodejs.org/api/addons….