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.
npm install node-cmakeVerified import paths — ran on the pinned version, not inferred.
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.
Remove `find_package(NodeJS)` from your `CMakeLists.txt`. Instead, include the copied `NodeJS.cmake` script directly using `include(NodeJS.cmake)` after running `ncmake update` to copy the file to your project.
Update your build scripts (e.g., `package.json` scripts) to use the new `node-gyp` compatible syntax, such as `ncmake rebuild` (which performs clean, configure, and build) instead of the old `--build` flag. Refer to the `node-cmake` manual for a complete list of new commands.
Always run `ncmake update` after updating `node-cmake` to a new version (especially major ones) to ensure your project benefits from the latest configuration script changes and bug fixes.
Users targeting NW.js should be aware of potential download and integrity validation issues. Consider alternative build systems or manual dependency management for NW.js until the upstream server compliance is addressed.
Ensure `node-cmake` is listed in your `devDependencies` in `package.json` and run `npm install`. When invoking `ncmake` in scripts, use `npm run <script-name>` or `npx ncmake` to ensure the binary is resolved correctly.
Remove the `find_package(NodeJS)` call from your `CMakeLists.txt`. Instead, add `include(NodeJS.cmake)` and ensure you have run `ncmake update` to copy the necessary `NodeJS.cmake` file into your project.
Update your `package.json` scripts or any direct CLI invocations to use the new `node-gyp`-compatible commands, such as `ncmake rebuild`.
Run `ncmake update` from your project's root directory. This command copies `NodeJS.cmake` into your project, making it available for inclusion in `CMakeLists.txt`.