This guide covers everything you need to know about how to , set it up, and use it to compile your native modules. What is CMake.js?
is a Node.js build tool that uses CMake to compile native addons. It works similarly to node-gyp but leverages the power of the CMake build system, making it easier to manage complex dependencies and cross-platform builds. Step 1: Prerequisites
For most projects, it is better to install it as a development dependency. This ensures that everyone working on your project uses the same version: npm install --save-dev cmake-js Use code with caution. Step 3: Setting Up Your Project download cmake-js
cmake_minimum_required(VERSION 3.15) project(my_native_addon) add_definitions(-DNAPI_VERSION=3) include_directories(${CMAKE_JS_INC}) add_library(${PROJECT_NAME} SHARED src/main.cpp ${CMAKE_JS_SRC}) set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node") target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB}) Use code with caution.
Download and install CMake from the official downloads page . Ensure it is added to your system PATH . C++ Compiler: This guide covers everything you need to know
If you installed it locally and didn't add the script to package.json , use: npx cmake-js compile Use code with caution. Why Choose CMake.js over node-gyp?
If your project depends on other C++ libraries, CMake makes it trivial to include them as submodules. Conclusion It works similarly to node-gyp but leverages the
GCC or Clang (e.g., sudo apt install build-essential ). Step 2: How to Download and Install CMake.js