{"library":"node-cmake","title":"CMake-based Build System for Node.js Native Modules","description":"node-cmake is a build system for Node.js native modules that leverages CMake (version 3.1 or newer) as an alternative to the default node-gyp. The current stable version is 2.5.1. Major version 2.0 introduced a complete rewrite, aiming for drop-in execution compatibility with the `node-gyp` binary and standardization of its output format. Key differentiators include its reliance on CMake for greater build flexibility, a simplified and portable configuration script (`NodeJS.cmake`), and improved handling of Node.js variants. While its primary interaction is via CLI, it provides a JavaScript module for locating built native addons. The project's release cadence is not strictly defined, but major versions can introduce significant breaking changes, as seen with the 2.0 rewrite.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-cmake"],"cli":{"name":"node-cmake","version":null}},"imports":["import { find } from 'node-cmake/lib/binding';","import { ncmake } from 'node-cmake';","import { commands } from 'node-cmake';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"```json\n// package.json\n{\n  \"name\": \"my-native-addon-project\",\n  \"version\": \"1.0.0\",\n  \"private\": true,\n  \"scripts\": {\n    \"postinstall\": \"ncmake update\",\n    \"build\": \"ncmake rebuild\",\n    \"start\": \"node index.js\"\n  },\n  \"devDependencies\": {\n    \"node-cmake\": \"^2.5.1\"\n  }\n}\n```\n\n```cmake\n// CMakeLists.txt\ncmake_minimum_required(VERSION 3.1)\n\nproject(my_module VERSION 1.0.0)\n\ninclude(NodeJS.cmake)\nnodejs_init()\n\nadd_nodejs_module(${PROJECT_NAME} my_module.cc)\n```\n\n```cpp\n// my_module.cc\n#include <napi.h>\n\nNapi::String GreetMethod(const Napi::CallbackInfo& info) {\n  Napi::Env env = info.Env();\n  return Napi::String::New(env, \"world from C++!\");\n}\n\nNapi::Object Init(Napi::Env env, Napi::Object exports) {\n  exports.Set(Napi::String::New(env, \"greet\"), Napi::Function::New(env, GreetMethod));\n  return exports;\n}\n\nNODE_API_MODULE(my_module, Init)\n```\n\n```javascript\n// index.js\nconst path = require('path');\nconst { find } = require('node-cmake').binding;\n\nasync function main() {\n  try {\n    // Locate the built native module using node-cmake's helper\n    const modulePath = find(process.cwd(), 'my_module.node');\n    if (!modulePath) {\n      console.error(\"Error: Native module 'my_module.node' not found.\\nMake sure to run 'npm install' to build the native addon.\");\n      process.exit(1);\n    }\n    const addon = require(modulePath);\n    console.log(`Hello ${addon.greet()}`); // Expected: Hello world from C++!\n  } catch (error) {\n    console.error(\"Failed to load or execute native module:\", error);\n    process.exit(1);\n  }\n}\n\nmain();\n```","lang":"typescript","description":"This quickstart demonstrates how to set up a Node.js native addon project using `node-cmake`. It includes `package.json` configurations, a `CMakeLists.txt` build script, a simple C++ source file (`my_module.cc`), and a JavaScript file (`index.js`) that uses the `node-cmake` helper to load and interact with the compiled native module. Run `npm install` to build the module, then `npm start` to execute the JavaScript example.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}