Quick guide on setting up debugging for node-gyp. Includes debugging via terminal and a visual studio code config for easy setup. This assumes you have created a working node-gyp project and just want to get the debugging working. If not start here for a basic setup.
NOTE: This is via Linux as we are using gdb but if you using another OS; lookup how the specific debugger works and apply a similar principle.
- Click here: If you already know how to use GDB in terminal/commandline and want to get it working.
- Click here: If your not sure or would like an IDE to do the heavy lifting aka Visual Studio Code.
Terminal
Make sure the index.js (or whatever file is doing the require) has references the debug build when debugging. You can point the require directly at the debug build or you can this by setting an environmental variable like so:
const { hello } = process.env.NODE_ENV === "dev" ? require('./build/Debug/hello') : require('./build/Release/hello');
hello();
You need to generate either a debug build of node-gyp: node-gyp build --debug
Or a release build: node-gyp build
Then run gdb as standard with the parameters:
gdb --args node index.js
Set the environmental variable once gdb is running with the commmand:
set environment NODE_ENV = dev
Then simply type run
and away you go debugging.
NOTE: I didn’t figure out how to link the source, I’ll try to remember to update this if I figure it out.
If you have trouble
Find out where node is installed with which node
For me it was inside my .nvm folder as i use node version manger.
Then run using /absolute or ~/relative paths, like this:
gdb --args /home/USER_NAME/.nvm/versions/node/v8.9.4/bin/node /Path/To/index.js
Visual Studio Code
Make sure the index.js has references the debug build when debugging. I did this by setting an environmental variable like so:
const { hello } = process.env.NODE_ENV === "dev" ? require('./build/Debug/hello') : require('./build/Release/hello');
hello();
Create a task in .vscode/tasks.json to create the debug build in node-gyp like:
{
"version": "2.0.0",
"tasks": [
{
"label": "debug build",
"type": "shell",
"command": "node-gyp build --debug",
"group": {
"kind": "test",
"isDefault": true
}
}
]
}
Then setup the .vscode/launch.json.
- Things that are different from a standard gdb launch:
- The program is the node binary
- The args is the index.js we plan to run
- the environment is setting the envionmental variable flag so we reference the debug build via the require we put inside the index.js file
- Prelaunch is the task we just created
Here is the full launch.json for reference:
NOTE: I use nvm
to manage my node versions.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/home/USER_NAME/.nvm/versions/node/v8.9.4/bin/node",
"args": ["${workspaceFolder}/index.js"],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [{"name":"NODE_ENV", "value":"dev" }],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "debug build"
}
]
}