DEV Community

loading...

Inspect your node application on a different machine

attilavm profile image Attila Molnar ・2 min read

Problem

You wrote a node app, which works on your workstation but not on your server, and you plan to use your Chrome to debug the remote app.

Warning

Debugging anything on a public server, especially on a production server, is an extremely typical way to a monday morning mistake. If your server runs in a container or in a virtual machine, you can use Docker or Vagrant to run an equivalent instance on your local machine, and the method below will still work, it will be even faster without the extra networking overhead.

Solution

Thankfully our problem is fairly easy to solve with the all powerful SSH command, which among other breathtaking features allows you to forward TCP connections from your local machine to your host or vica-versa. So we will forward connections directed to the node debugging port on our workstation to our host.

1) On the host start your app with the --inspect switch

$ node --inspect myapp

In the output you should seek a line like this.

Debugger listening on ws://127.0.0.1:9229/e9580cde-b1ee-4ed1-a23b-e8178390b1b9

It tells us two important things. First the IP 127.0.0.1 ensures us that the debugger won't accept any connection from the network, which may have some shady characters lurking around, especially if you are doing this on the Internet against my warning, then let me hold your beer in awe.

Second the 9229 number just after the IP is the port number used by the debugger, eager to accept a connection from our local chrome.

2) Type the following command on the local machine.

 ssh -L9229:localhost:9229 user@remote_hostname_or_ip

It will forward any TCP connections directed to your local 9229 port to the same port on the host.

3) If everything is fine, now you can debug your remote app the same way as the local one. Just put this line to your URL bar.

chrome://inspect/#devices

The following page will be presented to you.

Screenshot showing Chrome's interface for inspecting devices

4) Just click on the inspect

5) Hunt the bugs.

A comic from XKCD where the programmer tells a story to his friend, that after a long debugging session found the sword of Martin the warrior, his friend assumes that he switched the puzzles in his story
image source: xkcd comic

Discussion (0)

pic
Editor guide