Before starting to use N-API you need to assure you have the following prerequisites:
Node.JS see: Installing Node.js
Node.js native addon build tool
To use N-API in a native module:
package.json
: "dependencies": {
"node-addon-api": "*",
}
binding.gyp
: 'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
binding.gyp
: 'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'xcode_settings': {
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.7',
},
'msvs_settings': {
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
},
Alternatively, disable use of C++ exceptions in N-API:
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
binding.gyp
file: ['OS=="mac"', {
'cflags+': ['-fvisibility=hidden'],
'xcode_settings': {
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
}
}]
napi.h
in the native module code.
To ensure only ABI-stable APIs are used, DO NOT include
node.h
, nan.h
, or v8.h
.#include "napi.h"
At build time, the N-API back-compat library code will be used only when the targeted node version does not have N-API built-in.
The preprocessor directive NODE_ADDON_API_DISABLE_DEPRECATED
can be defined at
compile time before including napi.h
to skip the definition of deprecated APIs.