Reading time: 2 minutes.
How to debug a Node.js application remotely in VSCode
Debugging a remote Node.js application in VSCode is an elementary procedure. Here it is a small “how to” in case you are struggling with it.
Because I know you want just the answer, let’s skip for now all the background info.
- Run the remote application with the
--inspect=PORTflag. For example,
- In the app logs you should see something like
Debugger listening on ws://127.0.0.1:9228/408a0f96-c43c-4b97-8bc6-a25eb55f9125. Save this address.
- On your local machine use SSH tunneling to map the remote port
PORTinto a local
PORTwith this command:
ssh -L <LOCAL PORT>:127.0.0.1:<REMOTE PORT> <USERNAME>@<HOST>. For example:
- Now, with that terminal windows open, go to VSCode and reate a
.vscode/launch.jsonfile with this content:
- Run the debug configuration on VSCode.
- You should now be able to connect to the remote app. Check on the status bar for confirmation.
A bit more info (if you want)
Every time I get a bug that I cannot reproduce locally, the only solution is to plug into the remote application and try to debug it. In my opinion, this is debugging 101, however I am surprised of how many people seem think that remote debugging is some kind of black magic. As you can see from the step above, it is a quite simple procedure.
The real game changer is SSH tunneling. With it, if you have SSH access to the remote machine then you can do remote debugging like it was local. This allows you to forget about complicated firewall and network rules (that you may or may not access or change).
I hope this is usefull. Cheers.