N-API

May 2, 2019 ยท View on GitHub

Platform Support

The following chart shows the availability of each N-API function on each platform. The functions point to the related part of the official N-API documentation.

Supported featuresLinux
(Ubuntu)
napi_add_env_cleanup_hookO
napi_async_complete_callbackO
napi_async_destroyO
napi_async_execute_callbackO
napi_async_initO
napi_callbackO
napi_callback_infoO
napi_call_functionO
napi_cancel_async_workO
napi_close_callback_scopeO
napi_close_escapable_handle_scopeO
napi_close_handle_scopeO
napi_coerce_to_boolO
napi_coerce_to_numberO
napi_coerce_to_objectO
napi_coerce_to_stringO
napi_create_arrayO
napi_create_arraybufferO
napi_create_array_with_lengthO
napi_create_async_workO
napi_create_bufferO
napi_create_buffer_copyO
napi_create_dataviewO
napi_create_doubleO
napi_create_errorO
napi_create_externalO
napi_create_external_arraybufferO
napi_create_external_bufferO
napi_create_functionO
napi_create_int32O
napi_create_int64O
napi_create_objectO
napi_create_promiseO
napi_create_range_errorO
napi_create_referenceO
napi_create_string_latin1O
napi_create_string_utf16O
napi_create_string_utf8O
napi_create_symbolO
napi_create_threadsafe_functionO
napi_create_typedarrayO
napi_create_type_errorO
napi_create_uint32O
napi_define_classO
napi_define_propertiesO
napi_delete_async_workO
napi_delete_elementO
napi_delete_propertyO
napi_delete_referenceO
napi_envO
napi_escapable_handle_scopeO
napi_escape_handleO
napi_extended_error_infoO
napi_fatal_errorO
napi_fatal_exceptionO
napi_finalizeO
napi_get_and_clear_last_exceptionO
napi_get_arraybuffer_infoO
napi_get_array_lengthO
napi_get_booleanO
napi_get_buffer_infoO
napi_get_cb_infoO
napi_get_dataview_infoO
napi_get_elementO
napi_get_globalO
napi_get_last_error_infoO
napi_get_named_propertyO
napi_get_new_targetO
napi_get_node_versionO
napi_get_nullO
napi_get_propertyO
napi_get_property_namesO
napi_get_prototypeO
napi_get_reference_valueO
napi_get_typedarray_infoO
napi_get_undefinedO
napi_get_uv_event_loopO
napi_get_value_boolO
napi_get_value_doubleO
napi_get_value_externalO
napi_get_value_int32O
napi_get_value_int64O
napi_get_value_string_latin1O
napi_get_value_string_utf16O
napi_get_value_string_utf8O
napi_get_value_uint32O
napi_get_versionO
napi_handle_scopeO
napi_has_elementO
napi_has_named_propertyO
napi_has_own_propertyO
napi_has_propertyO
napi_instanceofO
napi_is_arrayO
napi_is_arraybufferO
napi_is_bufferO
napi_is_dataviewO
napi_is_errorO
napi_is_exception_pendingO
napi_is_promiseO
napi_is_typedarrayO
napi_make_callbackO
napi_new_instanceO
napi_open_callback_scopeO
napi_open_escapable_handle_scopeO
napi_open_handle_scopeO
napi_property_attributesO
napi_property_descriptorO
napi_queue_async_workO
napi_refO
napi_reference_refO
napi_reference_unrefO
napi_reject_deferredO
napi_remove_env_cleanup_hookO
napi_remove_wrapO
napi_resolve_deferredO
napi_set_elementO
napi_set_named_propertyO
napi_set_propertyO
napi_statusO
napi_strict_equalsO
napi_throwO
napi_throw_errorO
napi_throw_range_errorO
napi_throw_type_errorO
napi_typedarray_typeO
napi_typeofO
napi_unwrapO
napi_valueO
napi_valuetypeO
napi_wrapO

The API does not support the following features:

N-API

Based on Node.js v10.15 (LTS)

N-API (pronounced N as in the letter, followed by API) is an API for building native Addons. N-API is generally used to create and manipulate JavaScript values. You can read more about N-API here.

Our implementation works with either ES2015 enabled or disabled. All of the N-API ES2015 features throw an exception when IoT.js isn't compiled with ES2015 yet they are used.

Building N-API with IoT.js

You need to append the --n-api parameter to the build command (e.g. tools/build.py --n-api). This automatically adds the N-API module to IoT.js.

To run N-API test you should also append --n-api after the testrunner.py.

Building a simple module

node-gyp

C++ code needs to be compiled into executable form whether it be as an object file to linked with others, a shared library, or a standalone executable.

The main reason for this is that we need to link to the Node.js dependencies and headers correctly, another reason is that we need a cross platform way to build C++ source into binary for the target platform.

Until now node-gyp is the de-facto standard build tool for writing Node.js addons. It's based on Google's gyp build tool, which abstract away many of the tedious issues related to cross platform building.

node-gyp uses a file called binding.gyp that is located on the root of your addon project.

binding.gyp file, contains all building configurations organized with a JSON like syntax. The most important parameter is the target that must be set to the same value used on the initialization code of the addon as in the examples reported below:

binding.gyp

{
  "targets": [
    {
      # myModule is the name of your native addon
      "target_name": "myModule",
      "sources": ["src/my_module.cc", ...],
      ...
  ]
}

my_module.cc

#include <napi.h>

// ...

/**
* This code is our entry-point. We receive two arguments here, the first is the
* environment that represent an independent instance of the JavaScript runtime,
* the second is exports, the same as module.exports in a .js file.
* You can either add properties to the exports object passed in or create your
* own exports object. In either case you must return the object to be used as
* the exports for the module when you return from the Init function.
*/
Napi::Object Init(Napi::Env env, Napi::Object exports) {

  // ...

  return exports;
}

/**
* This code defines the entry-point for the Node addon, it tells Node where to go
* once the library has been loaded into active memory. The first argument must
* match the "target" in our *binding.gyp*. Using NODE_GYP_MODULE_NAME ensures
* that the argument will be correct, as long as the module is built with
* node-gyp (which is the usual way of building modules). The second argument
* points to the function to invoke. The function must not be namespaced.
*/
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)

node-gyp reference

Sometimes finding the right settings for binding.gyp is not easy so to accomplish at most complicated task please refer to:

  • GYP documentation
  • node-gyp wiki The most important parameter is the target that must be set to the same value used on the initialization code of the addon. To use the N-API functions you need to include node-api.h in your native module.