During the development of any software application, sometimes a developer may need to debug the code to understand the flow of execution or even to fix a bug. I have seen JavaScript developers prints the output/value on the console using console.log()
to debug the node application.
This is definitely not an efficient way to debug the code. You have to log the values in various places, hence you need to repeat console.log()
multiple times. Also, you need to take them off before committing your code. This would slow down the development time and mess up the code.
Debugging your node application using Chrome DevTools is one of the simple and efficient ways.
Here are steps to debug your node application.
1. Run the application using --inspect
flag.
Once your server up and running…
2. Open this URL in your Chrome browser : about:inspect
3. Click on Open dedicated DevTools for Node
You will get a dialog box where you need to provide your application end-point and click on Add connection
You can click on the Sources
tab and navigate to your application folder. You will be able to debug your code here.
In the below screenshot I have kept a breakpoint on line 8
where I’m finding out the parameter sent to the /number
route which is 30
.
You can debug your server-side JavaScript code just like how you debug your client-side JavaScript code.