N-API versions are additive and versioned independently from Node.js. Version 4 is an extension to version 3 in that it has all of the APIs from version 3 with some additions. This means that it is not necessary to recompile for new versions of Node.js which are listed as supporting a later version.

    1 2 3
    v6.x v6.14.2
    v8.x v8.6.0** v8.10.0 v8.11.2
    v9.x v9.0.0 v9.3.0 v9.11.0*
    ≥ v10.x all releases all releases all releases
    4 5 6 7
    v10.x v10.16.0 v10.17.0 v10.20.0
    v11.x v11.8.0
    v12.x v12.0.0 v12.11.0 v12.17.0 v12.19.0
    v13.x v13.0.0 v13.0.0
    v14.x v14.0.0 v14.0.0 v14.0.0 v14.12.0

    * N-API was experimental.

    ** Node.js 8.0.0 included N-API as experimental. It was released as N-API version 1 but continued to evolve until Node.js 8.6.0. The API is different in versions prior to Node.js 8.6.0. We recommend N-API version 3 or later.

    Each API documented for N-API will have a header named added in:, and APIs which are stable will have the additional header N-API version:. APIs are directly usable when using a Node.js version which supports the N-API version shown in N-API version: or higher. When using a Node.js version that does not support the N-API version: listed or if there is no N-API version: listed, then the API will only be available if #define NAPI_EXPERIMENTAL precedes the inclusion of node_api.h or js_native_api.h. If an API appears not to be available on a version of Node.js which is later than the one shown in added in: then this is most likely the reason for the apparent absence.

    The N-APIs associated strictly with accessing ECMAScript features from native code can be found separately in js_native_api.h and js_native_api_types.h. The APIs defined in these headers are included in node_api.h and node_api_types.h. The headers are structured in this way in order to allow implementations of N-API outside of Node.js. For those implementations the Node.js specific APIs may not be applicable.

    The Node.js-specific parts of an addon can be separated from the code that exposes the actual functionality to the JavaScript environment so that the latter may be used with multiple implementations of N-API. In the example below, addon.c and addon.h refer only to js_native_api.h. This ensures that addon.c can be reused to compile against either the Node.js implementation of N-API or any implementation of N-API outside of Node.js.

    addon_node.c is a separate file that contains the Node.js specific entry point to the addon and which instantiates the addon by calling into addon.c when the addon is loaded into a Node.js environment.

    1. // addon.h
    2. #ifndef _ADDON_H_
    3. #define _ADDON_H_
    4. #include <js_native_api.h>
    5. napi_value create_addon(napi_env env);
    6. #endif // _ADDON_H_
    1. // addon.c
    2. #include "addon.h"
    3. #define NAPI_CALL(env, call) \
    4. do { \
    5. napi_status status = (call); \
    6. if (status != napi_ok) { \
    7. const napi_extended_error_info* error_info = NULL; \
    8. napi_get_last_error_info((env), &error_info); \
    9. bool is_pending; \
    10. napi_is_exception_pending((env), &is_pending); \
    11. if (!is_pending) { \
    12. const char* message = (error_info->error_message == NULL) \
    13. ? "empty error message" \
    14. : error_info->error_message; \
    15. napi_throw_error((env), NULL, message); \
    16. return NULL; \
    17. } \
    18. } \
    19. } while(0)
    20. static napi_value
    21. DoSomethingUseful(napi_env env, napi_callback_info info) {
    22. // Do something useful.
    23. return NULL;
    24. }
    25. napi_value create_addon(napi_env env) {
    26. napi_value result;
    27. NAPI_CALL(env, napi_create_object(env, &result));
    28. napi_value exported_function;
    29. NAPI_CALL(env, napi_create_function(env,
    30. "doSomethingUseful",
    31. NAPI_AUTO_LENGTH,
    32. DoSomethingUseful,
    33. NULL,
    34. &exported_function));
    35. NAPI_CALL(env, napi_set_named_property(env,
    36. result,
    37. "doSomethingUseful",
    38. exported_function));
    39. return result;
    40. }
    1. // addon_node.c
    2. #include <node_api.h>
    3. #include "addon.h"
    4. NAPI_MODULE_INIT() {
    5. // This function body is expected to return a `napi_value`.
    6. // The variables `napi_env env` and `napi_value exports` may be used within
    7. // the body, as they are provided by the definition of `NAPI_MODULE_INIT()`.
    8. return create_addon(env);
    9. }