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

Saransh Khobragade
Aug 10, 2019 · 3 min read

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


Image for post
Image for post

Start debugging Node with powerful VScode

node --inspect=2000 hello.js

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

Image for post
Image for post
Image for post
Image for post
>attach
Image for post
Image for post
Image for post
Image for post

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

const express = require('express')const app = express()let request = require('request');app.get('/checkapi', async (req, res) => {request({url: 'https://jsonplaceholder.typicode.com/todos/1',headers: {'Content-Type': 'application/json'},method: 'GET',json:true}, function (err, httpResponse, data) {if (err) {res.status(400).send({ "data": err })} else {res.status(200).send({ "data": data })}});});var server = require('http').createServer(app);server.listen(process.env.PORT || 8080);console.log('server running...at 8080');

Mark debug points

Image for post
Image for post

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

And here we go…..

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

you can use watch with custom expression also

Image for post
Image for post

Toggle within different breakpoints from BREAKPOINTS, or clear them.

Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

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)

https://medium.com/@saransh98/debugging-node-js-via-vs-code-with-inspect-brk-multiple-process-clusters-b7282d63bec7

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store