Skip to content

Instantly share code, notes, and snippets.

@pajtai
Last active May 29, 2023 12:13
Show Gist options
  • Save pajtai/081e0371e7366c79c0b9 to your computer and use it in GitHub Desktop.
Save pajtai/081e0371e7366c79c0b9 to your computer and use it in GitHub Desktop.
Debugging Node with Webstorm running on a remote server

Debugging node running on a remote server

Node uses a TCP interface for debugging, so if you can get a handle on the right port, you can debug apps running remotely. This means you can run through code on staging, Vagrant, etc. The following shows you how to start node with the debug flag and use an SSH tunnel to access the right port.

Things you need:

  • ssh access to the server
  • ability to restart node app with --debug flag or node-inspector installed on server

Debugging using Webstorm

  1. Stop the app

  2. Restart with --debug flag (and include any necessary env flags) (could setup a name pm2 for this)

    # example with an env variable sent int
    NODE_ENV=staging node --debug /home/node_user/my-app
  3. When it starts you should see something like debugger listening on 5858

  4. Setup webstorm

    1. Run > Debug... > Edit Configurations... > Add new configuration > Node.js Remote Debug
    2. Host : 127.0.0.1 - Port : 5858
  5. Open SSH Tunnel to gain access to servers port 5858.

    # open an ssh tunnel, send it to the bg, and wait 10 seconds for connections
    # once all connections are closed after 10 seconds then close the tunnel
    ssh -f -o ExitOnForwardFailure=yes -L 5858:127.0.0.1:5858 [email protected] sleep 10
  6. Make sure you checkout the same code on your local machine as on the remote server.

  7. Run the debugger in Webstorm (drop down in upper right next to the Bug icon - or the bug icon once you've run it once)

Debugging using Node Inspector

You can follow similar instruction for using node-inspector just use port 8080 instead of 5858 and open Chrome at: http://127.0.0.1:8080/?port=5858 (you might want to use the --debug-brk flag instead of the debug flag if you can't set a breakpoint in time. Instead of starting with the debug flag, you woul install node incpector on the server and start the app using that.

Last Steps

If needed don't forget to stop the app and restart without the debug flag.

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