🐞 Debugging NodeJS C++ addons using VS Code

I believe that programming in a new language or domain seems hard until you know how to debug properly.

I was recently working on writing a NodeJS addon using C++ and N-API. For most part I was able to get around with simple std::cout or printf statements for debugging, but it was taking too much time even for simplest of cases.

I really love the debug tools that come with VS Code. They really make your life so easy when working with NodeJS. Hence, I wanted to use the same debugging environment when working with C++ and NAPI for NodeJS.

I ll be showing how to set this up in a mac. But I think it should be similar for windows and linux.

If you want to know how to build C++ Addons using NAPI for NodeJS, you can take a look at my other post here:

Setting up

For the explanation I have created a basic nodejs addon here: https://github.com/master-atul/basic-node-addon

This example essentially has a hello world function in c++.

  • Take a clone of the project.
  • do npm install

First step is to setup the necessary scripts in the package.json

"scripts": {
"start": "node index.js",
"build:dev": "node-gyp -j 16 build --debug",
"build": "node-gyp -j 16 build",
"rebuild:dev": "node-gyp -j 16 rebuild --debug",
"rebuild": "node-gyp -j 16 rebuild",
"clean": "node-gyp clean"
}

Here,npm run build:dev will build a debug version of the node addon.
while npm run build will build the release version of the addon.

PS: One major point to note here is that when you run npm run build:dev the addon is generated in build/Debug/yournode.node and when you run npm run build the addon is generated in build/Release/yournode.node . Make sure you setup paths accordingly.

Setting up VSCode for debugging

We will be using lldb to debug the nodejs addon. 
First step is to install the lldb extension for the vscode. The best extension for that is https://github.com/vadimcn/vscode-lldb by Vadim Chugunov

Press Cmd+Shift+P to open up the command bar. Now there type open launch.json .

Now modify you launch.json to look like this

{
"version": "0.2.0",
"configurations": [{
"type": "lldb",
"request": "launch",
"name": "Launch Program",
"preLaunchTask": "npm: build:dev",
"program": "/absolute/path/to/node",
"args": [
"/absolute/path/to/your/index.js"
]
}]
}

Note: type:"lldb" will only work after you install the previously mentioned vscode extension

The above mentioned launch.json will add a launch configuration for debugging in vscode. It will first do preLaunchTask (which we will setup in a bit) and then launch the index.js using node with lldb debugger attached.

Setting up preLaunch Task

Again press cmd+shift+p and then type in configure task .
From the list select npm: build:dev . That should create a tasks.json file inside .vscode and add the npm task there. Now you are all set.

PS: I have left a sample of .vscode/tasks.json and .vscode/launch.json inside the __.vscode directory in the example repo.

https://github.com/master-atul/basic-node-addon/tree/master/__.vscode

Running and debugging

First step is to set breakpoints. Now go back to the file explorer and open up the C++ file where you want to setup the breakpoints.

Lets say we would like to setup a break point in cppsrc/test.cpp

Click on the debugging icon on the left pane.

And click on thelaunch program or Debug play button.

Thats it ! you should be able to see all the variables and call stack etc.
Hope this helps someone ! 🌮