Debugging NodeJs with VScode and Chrome
Debugging is a very important part of development lifecycle and having an intuitive debugging technique is very important to quickly find out the logical/syntactical errors in code.
Node apps can be debugged using console.log(), though this technique is highly inefficient and doesn't draw the real picture when doing real-time debugging, but its a workaround to quickly find the output in between functions.
Debugging using chrome
Easiest way to debug nodejs apps is by using the command below
node --inspect index.js
This considering that index.js is the starting point of the app. This will create a local debugging server. Next, we go to chrome and open chrome://inspect/#devices
ideally it should display a remote target for debugging and on click of inspect below the link we will be able to debug our nodejs app. The view looks similar to the one below.
Incase the remote target doesn't show up after starting the debug server, probably there is something wrong in chrome configuration, double check the value for “Discover network targets/configure”, it should be the same as below, if not then add those manually.
Debugging using VSCode
VSCode has inbuilt support for node app debugging. Before proceeding further make sure that Debug toggle auto attach is switched on for the editor.
We need to run the commands below in VScode terminal to start a local debug server in VScode.
If you want to attach the VS Code debugger to a NodeJs program, launch NodeJs in VS Code’s integrated terminal as follows:
node --inspect index.js
or if the program should not start running but must wait for the debugger to attach:
node --inspect-brk index.js