18c339a94Sopenharmony_ci# Setup
28c339a94Sopenharmony_ci
38c339a94Sopenharmony_ci## Prerequisites
48c339a94Sopenharmony_ci
58c339a94Sopenharmony_ciBefore starting to use **Node-API** you need to assure you have the following
68c339a94Sopenharmony_ciprerequisites:
78c339a94Sopenharmony_ci
88c339a94Sopenharmony_ci* **Node.JS** see: [Installing Node.js](/)
98c339a94Sopenharmony_ci
108c339a94Sopenharmony_ci* **Node.js native addon build tool**
118c339a94Sopenharmony_ci
128c339a94Sopenharmony_ci  - **[node-gyp](node-gyp.md)**
138c339a94Sopenharmony_ci
148c339a94Sopenharmony_ci## Installation and usage
158c339a94Sopenharmony_ci
168c339a94Sopenharmony_ciTo use **Node-API** in a native module:
178c339a94Sopenharmony_ci
188c339a94Sopenharmony_ci  1. Add a dependency on this package to `package.json`:
198c339a94Sopenharmony_ci
208c339a94Sopenharmony_ci     ```json
218c339a94Sopenharmony_ci       "dependencies": {
228c339a94Sopenharmony_ci         "node-addon-api": "*",
238c339a94Sopenharmony_ci       }
248c339a94Sopenharmony_ci     ```
258c339a94Sopenharmony_ci
268c339a94Sopenharmony_ci  2. Decide whether the package will enable C++ exceptions in the Node-API
278c339a94Sopenharmony_ci     wrapper, and reference this package as a dependency in `binding.gyp`.
288c339a94Sopenharmony_ci     The base ABI-stable C APIs do not throw or handle C++ exceptions, but the
298c339a94Sopenharmony_ci     Node-API C++ wrapper classes may _optionally_
308c339a94Sopenharmony_ci     [integrate C++ and JavaScript exception-handling
318c339a94Sopenharmony_ci     ](/node-addon-api/blob/HEAD/doc/error_handling.md).
328c339a94Sopenharmony_ci
338c339a94Sopenharmony_ci     To use without C++ exceptions, add the following to `binding.gyp`:
348c339a94Sopenharmony_ci
358c339a94Sopenharmony_ci     ```gyp
368c339a94Sopenharmony_ci       'dependencies': [
378c339a94Sopenharmony_ci         "<!(node -p \"require('node-addon-api').targets\"):node_addon_api",
388c339a94Sopenharmony_ci       ],
398c339a94Sopenharmony_ci     ```
408c339a94Sopenharmony_ci
418c339a94Sopenharmony_ci     To enable that capability, add an alternative dependency in `binding.gyp`:
428c339a94Sopenharmony_ci
438c339a94Sopenharmony_ci     ```gyp
448c339a94Sopenharmony_ci       'dependencies': [
458c339a94Sopenharmony_ci         "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
468c339a94Sopenharmony_ci       ],
478c339a94Sopenharmony_ci     ```
488c339a94Sopenharmony_ci
498c339a94Sopenharmony_ci     If you decide to use node-addon-api without C++ exceptions enabled, please
508c339a94Sopenharmony_ci     consider enabling node-addon-api safe API type guards to ensure the proper
518c339a94Sopenharmony_ci     exception handling pattern:
528c339a94Sopenharmony_ci
538c339a94Sopenharmony_ci     ```gyp
548c339a94Sopenharmony_ci       'dependencies': [
558c339a94Sopenharmony_ci         "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_maybe",
568c339a94Sopenharmony_ci       ],
578c339a94Sopenharmony_ci     ```
588c339a94Sopenharmony_ci
598c339a94Sopenharmony_ci  3. If you would like your native addon to support OSX, please also add the
608c339a94Sopenharmony_ci     following settings in the `binding.gyp` file:
618c339a94Sopenharmony_ci
628c339a94Sopenharmony_ci       ```gyp
638c339a94Sopenharmony_ci       'conditions': [
648c339a94Sopenharmony_ci         ['OS=="mac"', {
658c339a94Sopenharmony_ci             'cflags+': ['-fvisibility=hidden'],
668c339a94Sopenharmony_ci             'xcode_settings': {
678c339a94Sopenharmony_ci               'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
688c339a94Sopenharmony_ci             }
698c339a94Sopenharmony_ci         }]
708c339a94Sopenharmony_ci       ]
718c339a94Sopenharmony_ci       ```
728c339a94Sopenharmony_ci
738c339a94Sopenharmony_ci  4. Include `napi.h` in the native module code.
748c339a94Sopenharmony_ci     To ensure only ABI-stable APIs are used, DO NOT include
758c339a94Sopenharmony_ci     `node.h`, `nan.h`, or `.h`.
768c339a94Sopenharmony_ci
778c339a94Sopenharmony_ci     ```C++
788c339a94Sopenharmony_ci     #include "napi.h"
798c339a94Sopenharmony_ci     ```
808c339a94Sopenharmony_ci
818c339a94Sopenharmony_ciAt build time, the Node-API back-compat library code will be used only when the
828c339a94Sopenharmony_citargeted node version *does not* have Node-API built-in.
838c339a94Sopenharmony_ci
848c339a94Sopenharmony_ciThe preprocessor directive `NODE_ADDON_API_DISABLE_DEPRECATED` can be defined at
858c339a94Sopenharmony_cicompile time before including `napi.h` to skip the definition of deprecated APIs.
868c339a94Sopenharmony_ci
878c339a94Sopenharmony_ciBy default, throwing an exception on a terminating environment (eg. worker
888c339a94Sopenharmony_cithreads) will cause a fatal exception, terminating the Node process. This is to
898c339a94Sopenharmony_ciprovide feedback to the user of the runtime error, as it is impossible to pass
908c339a94Sopenharmony_cithe error to JavaScript when the environment is terminating. In order to bypass
918c339a94Sopenharmony_cithis behavior such that the Node process will not terminate, define the
928c339a94Sopenharmony_cipreprocessor directive `NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS`.
93