{"library":"node-addon-api","title":"Node-Addon-API C++ Wrapper for Node.js Native Addons","description":"Node-Addon-API (N-API) is a C++ wrapper library that significantly simplifies the development of Node.js native add-ons. It provides a higher-level, more idiomatic C++ interface over the raw C-based Node-API, leveraging modern C++ features like RAII (Resource Acquisition Is Initialization) and exceptions for safer and more robust native module development. This abstraction layer helps manage memory and resources automatically, reducing common errors associated with manual memory management in C. The current stable version is 8.7.0, with minor feature and bugfix releases occurring every few months. Its primary differentiator is making Node.js native module development accessible to C++ developers by providing familiar C++ paradigms, while maintaining ABI stability across Node.js major versions, ensuring compiled add-ons continue to work without recompilation against newer Node.js releases. It is the recommended path for new native addon development.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-addon-api"],"cli":null},"imports":["const myAddon = require('./build/Release/myaddon.node');","import myAddon from './build/Release/myaddon.node';","import { createRequire } from 'module'; const require = createRequire(import.meta.url); const myAddon = require('./build/Release/myaddon.node');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"/* C++ source file: hello.cc */\n#include <napi.h>\n\n// Simple synchronous method that returns a string\nNapi::String Method(const Napi::CallbackInfo& info) {\n  Napi::Env env = info.Env();\n  return Napi::String::New(env, \"world\");\n}\n\n// Initialize the addon\nNapi::Object Init(Napi::Env env, Napi::Object exports) {\n  exports.Set(Napi::String::New(env, \"hello\"),\n              Napi::Function::New(env, Method));\n  return exports;\n}\n\n// Register the addon module\nNODE_API_MODULE(hello, Init)\n\n/* binding.gyp (build configuration for node-gyp) */\n{\n  \"targets\": [\n    {\n      \"target_name\": \"hello\",\n      \"sources\": [ \"hello.cc\" ],\n      \"include_dirs\": [\n        \"<!@(node -p \\\"require('node-addon-api').include\\\")\"\n      ],\n      \"cflags!\": [ \"-fno-exceptions\" ],\n      \"cflags_cc!\": [ \"-fno-exceptions\" ],\n      \"defines\": [ \"NAPI_CPP_EXCEPTIONS\" ],\n      \"libraries\": []\n    }\n  ]\n}\n\n/* JavaScript usage: index.js */\n// To build the addon: run `node-gyp rebuild` in your terminal.\n// This will create a `build/Release/hello.node` file.\n\nconst addon = require('./build/Release/hello.node');\n\nconsole.log('addon.hello() =', addon.hello()); // Expected output: addon.hello() = world\n","lang":"typescript","description":"Demonstrates building a simple 'hello world' native addon in C++ using Node-Addon-API and loading/executing it from a Node.js JavaScript application. Includes the `binding.gyp` configuration.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}