Debugging Node.js via VS code with Attach (single process only)

Are you still relying on console.log() for tracking of your flow and debugging…

Start debugging Node with powerful VScode

  1. Method 1:(Attach)(light/Fast debugging) Till now you must be starting your node project by hitting “node your.js”,try hitting instead

You can use any terminal you want you can attach your debugger to 2000.

  • Check on left there is bug sign on VS code click on that its debug mode.
  • Click on setting icon of launch program.
  • Add configuration.
  • Click on “Node.js: Attach”
  • Change port to 2000 as we are running our file on 2000 port so we need to listen to 2000 port.
  • Add “restart:true” also like below and give name also.
  • After hitting save option of Testing debugging will be showing on top left corner dropdown.
  • CMD+p type
  • Hit it ,you need “Auto-attach : on” only oncs ,you can check this on down right corner.
  • Now select play button of Testing debugging on top left corner

Here is a dummy code of hello.js a simple node.js server

Mark debug points

Hit with postman GET http://localhost:8080/checkapi

And here we go…..

Hover over variable to see values…

Use Play button to jump to next debug point and debug line by line with next right button of it

You can also use DEBUG console at bottom and try out various js function with raw data.

you can use watch with custom expression also

Toggle within different breakpoints from BREAKPOINTS, or clear them.

If you have used external terminal to run node server you can view same logs in debug console.

For microservice architecture or you need to jump to multiple repo you can start different service on different port 2000,2001..etc and make launch configuration according to them and debug.

You can use nodemon also that hit save debugger will attach automatically because we gave “restart:true”

For Debugging Cluster node multi process you will have to take Method 2:Inspect-brk (heavy but more advanced debugging)