Debugging in Node.js
Debugging is technique to find and resolve bugs/problems in our software/system. Debugging is one of the most important technique required during and after development. But if we are not able to debug correctly and easily then it will be big problem for us as we may waste a lot of time around this.
So in this article I will explain few techniques to easily debug in our node/javascript code:
- Chrome dev tools with inspect
- Debugger module( Inbuilt node js debugger module)
- VS Code Inbuilt debugger (To help during development)
- Console (console.log, console.table, console.time, console.timeEnd)
Chrome Dev Tools (Inspect)
First we need to start node server with inspect with below command:
node — inspect ${working_directory}/app.js (or index.js / or any main file)
Here as we can see we got debugger listening on ws://127.0.0.1:9229/ba43ec9e-9e4d-483f-bdf0–226f1cfcf2a5
Now open the chrome browser and in the new tab open about:inspect in the address bar:
Now below window will appear when we submit the about:inspect -
Now click on the “ Open dedicated DevTools for Node” link on the page. Below window will open:
As all the setup is completed , now we will start debugging. First step for this to add a break point in the code (break point is point where the flow of execution will be paused till we continue manually) and to add it just click on the line number and blue button type button will appear — it indicates the break point.
Now when we hit the application, flow of execution will be paused at this point.
Now if we want to add more break just add by clicking. Then when we click on continue button then control of execution will go the next break point. Also there is option of going step by step using arrow sign so which we can go the through full code line by line.
Also there is a “watch portion” in which we can add variables/objects from code and there corresponding value will be displayed once control passes through that variables.
2. Inbuilt Node.js Debugger Module
Node.js gives out of process debugging module accessible via TCP Based Protocol.
Get Rishabh Jain’s stories in your inbox
Join Medium for free to get updates from this writer.
To start working with this we need to put the debugger in the code where we want to put the break point.
Now start the application with node debug app.js
Now at first it will put breakpoint on its own and we need to type c and press enter to continue
Then after this it will stop at line where we have put the debugger. Press c to continue (Will go to next debug point if its there)
Few controls we can do with debugger module:
- cont or c — continue execution,
- next or n — step next,
- step or s — step in,
- out or o — step out,
- repl — to evaluate script’s context.
Now one more example where we want to see the data before the debugger (Break point), we have to use repl (read–eval–print loop) there.
Now we will open repl in debug mode only and simply output the data as shown below.
3. Debugging with VS Code
Open the source code in VS Code. Go to the debug ( like insect shape) option on the left side of VS Code.
Now click on the launch program and we will get option to add launch configuration for the debugging. Click add configuration as shown in below screenshot.
Just attach the node process or any according to need and modify node version if any specific node version we want to use in debugging.
Now as we are done with attaching launch config, just click on green button after debug from top and code will be started with debugging mode.
Now simply add the break point by clicking on the line number on the left side and we will get a red color round indicating the break point.
Now when we hit the corresponding api, execution flow will stop at this point and we can add as many break points as required. Also we can go step by step as provided in the options VS Code debugger.
Hoping this will be helpful stuff. In case of any query please connect with me https://www.linkedin.com/in/rishabh-jain-a5978583/ or drop a mail @rishabh171192@gmail.com









