CMake.js is good build tool alternative to node-gyp. CMake.js is based on the CMake tool which must be installed.
CMake.js requires that CMake is already installed. Installers are available on the CMake website.
macOS developers may find it more convenient to install CMake using Homebrew. With Homebrew installed, CMake can be installed using a
brew install cmakecommand.
You can verify your CMake installation with the command:
1 | cmake --version |
As a Node native module developer, you may find it convenient to install CMake.js as a global command line tool:
1 | npm install cmake-js -g |
You can verify your CMake.js installation with the command:
1 | cmake-js --version |
package.jsonYour package.json file needs to have a couple of entires for your native module to work with CMake.js.
Since your native module needs to be compiled using CMake.js on installation, the scripts propery of your package.json file needs an install entry to make this happen:
1 | "scripts": { |
It is unlikely that the users of your native module will have CMake.js installed as a global command line tool. Therefore, your project needs to declare a development dependency on CMake.js. This can be accomplished by entering this command:
1 | npm install cmake-js --save-dev |
An alternative is to manually add the development dependency to your package.json file:
1 | "devDependencies": { |
Here is a complete package.json file:
1 | { |
CMakeLists.txtNative module built on CMake.js have a CMakeLists.txt that describe how the module is to be built. The file serves the same purpose as the binding.gyp for projects tht use node-gyp.
In addition to the entries required for any CMake build, additional entries are required when building native modules.
Here are the lines required for all native modules built using CMake.js:
1 | project (napi-cmake-build-example) |
NAPI_VERSIONWhen building a native module based on N-API, it is important to decalre the miniumen N-API version against which your module is designed to work. For CMake.js, this is accomplished by adding a line like this to the CMakeLists.txt file:
1 | # define NPI_VERSION |
In the absence of other requirments, N-API version 3 is a good choice as this is the N-API version active when N-API left experimental status.
node-addon-apiAdditional configuration values are required for N-API modules based on node-addon-api.
node-addon-api requires C++11. These configuration lines at the top of the CMakeLists.txt file specify this requirement:
1 | cmake_minimum_required(VERSION 3.9) |
Modules based on node-addon-api include additional header files that are not part of Node itself. These lines instruct CMake.js where to find these files:
1 | # Include N-API wrappers |
Here is a complete CMakeLists.txt file for an N-API native module built on node-addon-api :
1 | cmake_minimum_required(VERSION 3.9) |
The CMake.js project home page.
The CMake project home page.
CMake documentation.
A working examples.