18c339a94Sopenharmony_ci# node-gyp
28c339a94Sopenharmony_ci
38c339a94Sopenharmony_ciC++ code needs to be compiled into executable form whether it be as an object
48c339a94Sopenharmony_cifile to linked with others, a shared library, or a standalone executable.
58c339a94Sopenharmony_ci
68c339a94Sopenharmony_ciThe main reason for this is that we need to link to the Node.js dependencies and
78c339a94Sopenharmony_ciheaders correctly, another reason is that we need a cross platform way to build
88c339a94Sopenharmony_ciC++ source into binary for the target platform.
98c339a94Sopenharmony_ci
108c339a94Sopenharmony_ciUntil now **node-gyp** is the **de-facto** standard build tool for writing
118c339a94Sopenharmony_ciNode.js addons. It's based on 's **gyp** build tool, which abstract away
128c339a94Sopenharmony_cimany of the tedious issues related to cross platform building.
138c339a94Sopenharmony_ci
148c339a94Sopenharmony_ci**node-gyp** uses a file called ```binding.gyp``` that is located on the root of
158c339a94Sopenharmony_ciyour addon project.
168c339a94Sopenharmony_ci
178c339a94Sopenharmony_ci```binding.gyp``` file, contains all building configurations organized with a
188c339a94Sopenharmony_ciJSON like syntax. The most important parameter is the  **target** that must be
198c339a94Sopenharmony_ciset to the same value used on the initialization code of the addon as in the
208c339a94Sopenharmony_ciexamples reported below:
218c339a94Sopenharmony_ci
228c339a94Sopenharmony_ci### **binding.gyp**
238c339a94Sopenharmony_ci
248c339a94Sopenharmony_ci```gyp
258c339a94Sopenharmony_ci{
268c339a94Sopenharmony_ci  "targets": [
278c339a94Sopenharmony_ci    {
288c339a94Sopenharmony_ci      # myModule is the name of your native addon
298c339a94Sopenharmony_ci      "target_name": "myModule",
308c339a94Sopenharmony_ci      "sources": ["src/my_module.cc", ...],
318c339a94Sopenharmony_ci      ...
328c339a94Sopenharmony_ci  ]
338c339a94Sopenharmony_ci}
348c339a94Sopenharmony_ci```
358c339a94Sopenharmony_ci
368c339a94Sopenharmony_ci### **my_module.cc**
378c339a94Sopenharmony_ci
388c339a94Sopenharmony_ci```cpp
398c339a94Sopenharmony_ci#include <napi.h>
408c339a94Sopenharmony_ci
418c339a94Sopenharmony_ci// ...
428c339a94Sopenharmony_ci
438c339a94Sopenharmony_ci/**
448c339a94Sopenharmony_ci* This code is our entry-point. We receive two arguments here, the first is the
458c339a94Sopenharmony_ci* environment that represent an independent instance of the JavaScript runtime,
468c339a94Sopenharmony_ci* the second is exports, the same as module.exports in a .js file.
478c339a94Sopenharmony_ci* You can either add properties to the exports object passed in or create your
488c339a94Sopenharmony_ci* own exports object. In either case you must return the object to be used as
498c339a94Sopenharmony_ci* the exports for the module when you return from the Init function.
508c339a94Sopenharmony_ci*/
518c339a94Sopenharmony_ciNapi::Object Init(Napi::Env env, Napi::Object exports) {
528c339a94Sopenharmony_ci
538c339a94Sopenharmony_ci  // ...
548c339a94Sopenharmony_ci
558c339a94Sopenharmony_ci  return exports;
568c339a94Sopenharmony_ci}
578c339a94Sopenharmony_ci
588c339a94Sopenharmony_ci/**
598c339a94Sopenharmony_ci* This code defines the entry-point for the Node addon, it tells Node where to go
608c339a94Sopenharmony_ci* once the library has been loaded into active memory. The first argument must
618c339a94Sopenharmony_ci* match the "target" in our *binding.gyp*. Using NODE_GYP_MODULE_NAME ensures
628c339a94Sopenharmony_ci* that the argument will be correct, as long as the module is built with
638c339a94Sopenharmony_ci* node-gyp (which is the usual way of building modules). The second argument
648c339a94Sopenharmony_ci* points to the function to invoke. The function must not be namespaced.
658c339a94Sopenharmony_ci*/
668c339a94Sopenharmony_ciNODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
678c339a94Sopenharmony_ci```
688c339a94Sopenharmony_ci
698c339a94Sopenharmony_ci## **node-gyp** reference
708c339a94Sopenharmony_ci
718c339a94Sopenharmony_ci  - [Installation](/package/node-gyp#installation)
728c339a94Sopenharmony_ci  - [How to use](/package/node-gyp#how-to-use)
738c339a94Sopenharmony_ci  - [The binding.gyp file](/package/node-gyp#the-bindinggyp-file)
748c339a94Sopenharmony_ci  - [Commands](/package/node-gyp#commands)
758c339a94Sopenharmony_ci  - [Command options](/package/node-gyp#command-options)
768c339a94Sopenharmony_ci  - [Configuration](/package/node-gyp#configuration)
778c339a94Sopenharmony_ci
788c339a94Sopenharmony_ciSometimes finding the right settings for ```binding.gyp``` is not easy so to
798c339a94Sopenharmony_ciaccomplish at most complicated task please refer to:
808c339a94Sopenharmony_ci
818c339a94Sopenharmony_ci- [GYP documentation](/index.md)
828c339a94Sopenharmony_ci- [node-gyp wiki](/node-gyp/wiki)
83