{"library":"node-gyp","title":"Node.js Native Addon Build Tool","description":"node-gyp is a crucial cross-platform command-line tool written in Node.js, designed specifically for compiling native addon modules for the Node.js runtime. Currently stable at version 12.2.0, it demonstrates a consistent release cadence with frequent minor and patch updates, often aligning with new Node.js and npm major versions to maintain compatibility. A key differentiator is its integration of a vendored gyp-next project, which was adapted from Chromium's build system, providing robust support for native addon development across various operating systems. It automatically handles downloading the correct Node.js development files (headers) for the target Node.js version, simplifying the build process. Unlike some misconceptions, node-gyp is not used to build Node.js itself, but rather the C/C++ components that Node.js applications rely on. It supports all current and LTS versions of Node.js, ensuring broad compatibility for native module maintainers.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-gyp"],"cli":{"name":"node-gyp","version":null}},"imports":["npm install -g node-gyp\nnode-gyp configure build","const { cli } = require('node-gyp/lib/node-gyp');\ncli();"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"/* addon.cc */\n#include <napi.h>\n\nNapi::String Method(const Napi::CallbackInfo& info) {\n  Napi::Env env = info.Env();\n  return Napi::String::New(env, \"world\");\n}\n\nNapi::Object Init(Napi::Env env, Napi::Object exports) {\n  exports.Set(Napi::String::New(env, \"hello\"), Napi::Function::New(env, Method));\n  return exports;\n}\n\nNODE_API_MODULE(addon, Init);\n\n/* binding.gyp */\n{\n  'targets': [\n    {\n      'target_name': 'addon',\n      'sources': [ 'addon.cc' ],\n      'include_dirs': [\n        '<!@(node -p \"require('node-addon-api').include\")'\n      ],\n      'dependencies': [\n        '<!@(node -p \"require('node-addon-api').gyp\")'\n      ],\n      'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],\n    }\n  ]\n}\n\n/* package.json */\n{\n  \"name\": \"my-native-addon\",\n  \"version\": \"1.0.0\",\n  \"description\": \"A simple native addon example\",\n  \"main\": \"index.js\",\n  \"scripts\": {\n    \"install\": \"node-gyp rebuild\"\n  },\n  \"dependencies\": {\n    \"node-addon-api\": \"^6.0.0\"\n  }\n}\n\n/* index.js */\nconst addon = require('./build/Release/addon');\nconsole.log(addon.hello()); // 'world'\n","lang":"javascript","description":"This example demonstrates how to set up a simple native Node.js addon using `node-gyp` and `node-addon-api`. It includes the C++ source, `binding.gyp` configuration, and `package.json` scripts to build and load the addon.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}