{"library":"nan","title":"Native Abstractions for Node.js (NAN)","description":"nan (Native Abstractions for Node.js) is a crucial C++ header-only library designed to provide a consistent compatibility layer for developing Node.js native add-ons. It expertly abstracts away the frequent and significant breaking changes introduced by updates to the V8 JavaScript engine and Node.js core APIs. This allows add-on developers to write C++ code that compiles and functions reliably across a broad spectrum of Node.js versions, spanning from legacy 0.8 releases up to the current Node.js 25. The current stable version is 2.26.2. Releases are generally aligned with new major Node.js LTS lines or critical V8 API shifts. Its primary differentiator is offering a stable C++ API surface, thereby eliminating the complex, version-specific conditional compilation macros developers would otherwise need to manage manually. Importantly, nan does not expose any direct JavaScript runtime APIs; its sole focus is on simplifying the development and maintenance of native modules.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install nan"],"cli":null},"imports":["\"<!(node -e \\\"require('nan')\\\")\"","#include <nan.h>","Nan::SetMethod(target, \"methodName\", MyMethod);"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"/* package.json */\n{\n  \"name\": \"nan-example-addon\",\n  \"version\": \"1.0.0\",\n  \"private\": true,\n  \"gypfile\": true,\n  \"dependencies\": {\n    \"nan\": \"^2.26.2\"\n  },\n  \"scripts\": {\n    \"install\": \"node-gyp rebuild\"\n  }\n}\n\n/* binding.gyp */\n{\n  \"targets\": [\n    {\n      \"target_name\": \"myaddon\",\n      \"sources\": [ \"src/addon.cc\" ],\n      \"include_dirs\": [\n        \"<!(node -e \\\"require('nan')\\\")\"\n      ]\n    }\n  ]\n}\n\n/* src/addon.cc */\n#include <nan.h>\n\nvoid Method(const Nan::FunctionCallbackInfo<v8::Value>& info) {\n  info.GetReturnValue().Set(Nan::New(\"world\").ToLocalChecked());\n}\n\nvoid Initialize(v8::Local<v8::Object> exports) {\n  Nan::SetMethod(exports, \"hello\", Method);\n}\n\nNODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)\n\n/* index.js */\nconst addon = require('./build/Release/myaddon.node');\nconsole.log(addon.hello()); // Outputs: world\n","lang":"javascript","description":"This quickstart demonstrates how to set up a basic Node.js native add-on using `nan`, including the `package.json`, `binding.gyp`, C++ source, and a simple JavaScript entry point. Run `npm install` and then `node index.js`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}