NodeJS C++ Addon

Step by step guide to creating a C++ addon for NodeJS applications using node-addon-api.

NodeJS C++ Addon

Introduction

This is a guide to creating custom C++ addons for NodeJS applications, as C++ in most cases has improved performance over javascript for computations. Also, C++ addons allow more access to platform specific functionality.

  1. Create NodeJS Project
  2. Install node-gyp and node-addon-api
  3. Create binding.gyp with project
  4. Edit package.json - add "gypfile" attribute
  5. Create index.js file
  6. Create C++ source files
  7. Execute Build Command
Create NodeJS Project

First create a NodeJS project to use for debugging the quickest way is to use the NodeJS init. Enter the relative information when promoted.

npm imit

This will create a package.json file in the directory using the inputs provided.

Install node-gyp and node-addon-api

To build C++ addon requires the packages node-gyp and node-addon-api which can be downloaded to the project directory using the nmp command:

npm install node-gyp -–save-dev
npm install node-addon-api

Note that node-gyp requires that python is installed, and the PATH environment variable to python.exe is set in windows. These commands will install node-gyp and node-addon-api in the project directory.

Create binding.gyp

Create a new file within the project directory called ‘binding.gyp’ and put the following contents with the file. Replace %targetName% with the name of your addon. This file is used by node-gyp, any C++ source and header files need to be added to the sources attribute, in this case only cppsrc/main.cpp is used. If your code uses external libraries you will add them to the libraries array, this example does not use external libraries, so this is left empty.

{
    "targets": [{
        "target_name": "%targetName%",
        "cflags!": [ "-fno-exceptions" ],
        "cflags_cc!": [ "-fno-exceptions" ],
        "sources": [
            "cppsrc/main.cpp"
        ],
        'include_dirs': [
            "<!@(node -p \"require('node-addon-api').include\")"
        ],
        'libraries': [],
        'dependencies': [
            "<!(node -p \"require('node-addon-api').gyp\")"
        ],
        'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ]
    }]
}
Edit package.json - add "gpyfile" attribute

Edit the package.json file and add the attribute "gypfile": true, after the line containing "main": "index.js". Also change the "scripts" object to include:

package.json file should now look some thing like this:

{
    "name": "nodejsaddon",
    "version": "1.0.0",
    "description": "NodeJS C++ Addon",
    "main": "index.js",
    "gypfile": true,
    "scripts": {
        "build": "node-gyp rebuild",
        "clean": "node-gyp clean"
    },
    "author": "Mr Steven J Baldwin",
    "license": "ISC",
    "devDependencies": {
        "node-gyp": "^7.1.0"
    },
    "dependencies": {
        "node-addon-api": "^3.0.0"
    }
    }
                              
Create index.js file

In the project directory add a new javascript file named “index.js” this will be the main entry point for the debug project. Add the following content the index.js file.

const exampleAdddon = require('./build/Release/%targetName%.node');

module.exports = exampleAdddon;
                                

Again replace %targetName% with your addon name.

Create C++ Source File

Create a directory to store the C++ files, this must be the same as what is inside the binding.gyp file. In this example the directory is called ‘cppsrc’. Within that directory create a file called main.cpp which will contain the following:

#include <napi.h>
Napi::Object InitAll(Napi::Env env, Napi::Object exports){
    Return exports;
}
NODE_API_MODULE(%targetName%, InitAll)
                                

Replace %targetName% with the name of your addon, this must be the same as what is in the binding.gyp.

Execute Build Command

At this point you should be able to build the project using the command:

npm run build

This command will build your NodeJS addon, at the moment the addon does not do anything we will create a function that returns a C++ string and another that subtracts two parameters pass from javascript.