Skip to content

Instantly share code, notes, and snippets.

@luciopaiva
Last active December 25, 2020 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luciopaiva/5984381db2b7ab61dc680f545b42dbe2 to your computer and use it in GitHub Desktop.
Save luciopaiva/5984381db2b7ab61dc680f545b42dbe2 to your computer and use it in GitHub Desktop.
Debugging remote Node.js processes

Debugging remote Node.js processes

There are 3 steps involved:

1. send a signal to the process so it enters debug mode

Connect to the remote machine, get the pid of the Node.js process and then run:

kill -usr1 <pid>

This will signal the Node.js process that it should turn into debug mode. Port 9229 should be open now.

2. use port forwarding to connect to remote process

On your local machine:

ssh -C2qTnN -L 9229:localhost:9229 <remote-machine>

Where <remote-machine> is the host where the Node.js process is running. It should be configured in your ~/.ssh/config file. This will open local port 9229, tunnelling it to remote por 9229 (the port where the debug server is listening).

3. debug using your local tools

After step 2, you can act as if the remote process' debug server is running on your local machine. Just use any tool you'd use to debug local processes.

3.1 Chrome

To use Chrome, go to chrome://inspect. Your remote process should appear there. It still don't know how to navigate through the sources and add breakpoints, but I used it to take heap snapshots and it was very useful.

3.2 Webstorm

Create a new Attach to Node.js/Chrome run configuration. Confirm it is set to connect to por 9229 and run it. Considering you have your project opened, you should now be able to add breakpoints to any part of the code.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment