By debugging is meant the traditional sense: The execution of a target program stops at every breakpoint or
debugger; statement, giving control to a distinct debugger process. The debugger allows to 1) set breakpoints 2) resume or control the execution of the program and 3) inspect the program state.
There are a few common contexts of debugging: The target program can be the main program, but also the tests. The debugger can be the Node CLI debugger utility but also your favourite IDE, Google Chrome or any compliant system. Using an IDE allows setting breakpoints without touching the code, increasing your efficiency by an order of magnitude.
In the following we review how to debug in those different contexts.
Debugging requires the target program to run on an environment which allows the execution to be controlled by a debugger. In our case, Node makes this possible when launched with one of the three inspect flag variants:
… with the Terminal
continue, ...: The full list of commands that are available via the debugger is in the debugger reference.
Debugging the Program
node inspect main.js
This variant using
inspect has the following effect: It 1) pauses the execution at the first line, 2) uses the Node built in CLI debugging utility as the debugger, and 3) attaches the Terminal to the utility.
Debugging the Tests
What if you need to debug your program using the tests as the entry point?
Testing frameworks usually provide with a wrapper around the
node binary to run the tests (e.g.
jest). It is specific to each framework how to get it to run
node in debug mode.
In the case of the Jest framework, it is documented here and it works this way: Instead of running
jest , run:
node inspect $(which jest) --runInBand
--runInBandruns the tests in a single threaded fashion.
$(which jest)returns the path of the
jestbinary. If it is installed locally to the project, it would be
… with a Remote Debugger
We saw how to debug the main program or the tests with the Terminal. This is arguably the traditional and most simple way to debug, but it is just a specific case of debugging. Any compliant program can attach to Node (if Node runs in debug mode) and act as a debugger.
To make this possible, run the program as following:
node --inspect-brk main.js
--inspect-brk doesn't involve the Node CLI debugging utility or the Terminal. Instead, it launches Node in debug mode but it expects a debugger to attach.
--inspect-brk variant pauses the execution at the first line of code. This gives the opportunity for a debugger to attach instead of the program running and possibly ending before the debugger even intervenes.
IDE as debugger
Debugging with an IDE allows declaring the breakpoints without modifying the code or using set breakpoints debugger commands. Instead, one can just click on the lines. Behind the scene, the IDE uses the inspector API provided by the runtime (Node) to register the breakpoints at startup.
VSCode, it is straightforward to configure and use the integrated debugger:
VSCodeby searching for debug node in the settings, and set
- Run the
nodecommand given above in the
View > Terminal).
- Use the
View > Debugpane and the
Debug Consoleto interact. You can set breakpoints by clicking on the left of the line numbers in the code.
As a side note,
VSCode allows to save launch configurations. This allows to launch the program or the tests with specific settings in one click from the
View > Debug pane. How to proceed is explained in the official documentation.
Google Chrome as debugger
Google Chrome also integrates a Node debugger:
- Run the
nodecommand given above.
- Open Google Chrome and navigate to
Devices > Open dedicated DevTools for Node.
When starting coding in a new language or environment, it’s nice to just jump to writing code and skip all boilerplate knowledge and DSLs.
Very quickly though, knowing the environment can save you a lot of time and anxiety. Managing the runtime versions (e.g. in Ruby), understanding the import / export system, knowing how to write and launch the tests, and debugging: Demystifying those operations can end up changing the way one works, improving development efficiency and fun by an order of magnitude.