Debugging Node.js via VS code with Inspect-brk (multiple process/clusters)

Saransh Khobragade
4 min readAug 10, 2019

--

Till now this is the best i got..

Method 2: Advanced but heavy debugging

In previous post you have learned to debug using attaching debugger to same port you run your server.

https://medium.com/@saransh98/debugging-node-js-via-vs-code-with-attach-single-process-only-87c50683a8ef

But attaching works fine if you are using single process in cluster architecture it fails,plus its an overhead to attach again and again.

But what if i tell you there is an advance way to debug which will start on click no need attach or any process just drop breakpoints and debug.

Start you node server file with

node --inspect-brk cluster.js

By default default debugger listen 9229 port if you are running single sever file don’t mention port but if you are using multiple server file run on different port as

node --inspect-brk=60001 hello.js
node --inspect-brk=60002 hello2.js

You can use nodemon also.Here is dummy code of cluster if you need

var cluster = require('cluster');if (cluster.isWorker) {console.log('I am a worker');} else {console.log('I am a master');cluster.fork();cluster.fork();}

Toggle auto attach :on as explained in previous article

That’s it folks…

Just drop your debugger points and you can debug easily.It work only if you are using integrated terminal.

In cluster your flow could be stopped at different process you can check at auto attach process list.

If your debugger is stuck somewhere you can also check at breakpoints manually.

Advanced debugging tricks:

Right click on debugger point edit it

  1. Expressions : you can give conditional statements like you can check parameters in you function if they are null breakpoint will become active, type expression hit enter.

2. Hit count : you can also give hit points also as 2,3 etc suppose your flow stops at breakpoint multiple times lets say 10 times but you need to check only at 8th time set hit count as 8, and enter.

3. Log point : you can also put logs these are just for debugging purpose its not saved in you flow.Suppose you need to check you flow from 20 files so in each file you will put an debugger and check plus you have to wait every sec at each point ,instead you can put log messages in 20 file all these will appear in your debug console in one or two secs if any one missed you know what to do..

You are still writing in console some commands , but if you more than 5 to 10 files you need to run it would be a waste of time you can automate that also with powerful VS code tasks.

Automating debugging with TASKS for one click start.

Your repo must be having .vscode folder till now you haven’t used it thinking of it waste maybe let’s roll with it now.

Inside .vscode you need to have tasks.json.

Its content will be like

{"version": "2.0.0","tasks": [{"label": "task1","type": "shell","command": ["cd ~/Desktop/work/NodeJs; nodemon --inspect-brk cluster.js"],"group": "test","presentation": {"reveal": "always","panel": "new"}}]}

You can understand inside command you can give any command you were typing in terminal earlier .You can give multiple commands in single line also just separate with “;”

For microservice architecture you can write small tasks and compound under on big compound task also you just need to write one more task like

{"label": "compound_task","dependsOn": ["task1","task2"],"problemMatcher": []}

Just give different port to different microservice like

nodemon --inspect-brk=2001 cluster.js
nodemon --inspect-brk=2000 avi.js

Now how to run tasks hit cmd+p then write “>task” hit “Run:task”.You will see task1, task2, compound_task hit compound_task. You will find

Both task triggered and you are good to go..

Wait i am not yet done yet one last magic trick also…

You can automate furthur by binding your compound task to spec key hit like “cmd+u”

Hit cmd+p enter “key” you will get

click on Open key board shortcuts, keybindings.json will open like

Give your custom commands and run your debugging task on finger tips.

NOTE: cmd+ctrl+k to kill current instance of terminal :)

Enjoy folks..

--

--