Remote debug node js application using Visual Studio Code

When your writing your node based application you may need to debug your code to identify the bugs, flow issues etc. There are many popular mechanisms out there to debug your node application. Likewise, remote debugging is another really important way of debugging your application.

“Remote debugging is the process of debugging a program running on a system different from the debugger. To start remote debugging, a debugger connects to a remote system over a network. The debugger can then control the execution of the program on the remote system and retrieve information about its state.”

Microsoft Visual studio code (vscode) is a quite popular editor based on Electron. Remote debugging feature is inbuilt with this editor, but you may need to follow several steps to get the actual benefit.

Prerequisites

Step 01: Setup your node app code

Open vscode and create the app.js file with below content OR you can open your existing code on the editor.

var msg = “Hello, Debugger Started”;
console.log(msg);
var x = 10;
console.log(x+1);

Step 02: Add Launch Configurations to app

Next step would be creating a launch configurations on visual studio code. To do that click on debug icon on the side bar.

visual studio code debugger

Click on cogwheel on left hand top position, then you can select “Node.js” environment.

select environment

Then vscode will automatically add launch.json to .vscode folder.

default launch.json

You can configure port and address as you desired.

“port”: 5858,
“address”: “localhost”,

Step 03: Run node js application in debug mode

Now go to console and type below command to run your node application in debug mode.

node  — debug-brk=5858 app.js

Step 04: Attach debugger

Back to vscode and select “Attach” from debug menu dropdown, then select run.

Viola….!

let me know if you are having any issue. You can simply change localhost to any IP address and check the power of VSCode remote debugging…!