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 ornode-inspector
installed on server
-
Stop the app
-
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
-
When it starts you should see something like
debugger listening on 5858
-
Setup webstorm
Run > Debug... > Edit Configurations... > Add new configuration > Node.js Remote Debug
- Host :
127.0.0.1
- Port :5858
-
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
-
Make sure you checkout the same code on your local machine as on the remote server.
-
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.
If needed don't forget to stop the app and restart without the debug flag.