First, you need to have a NodeJS application you want to debug. After that, you’ll need to install one global module:
npm install -g node-inspector
If you were able to follow till this point then you are, basically, all set. No, I didn’t forget to give more instructions or cryptic commands, that is all you need . Now, how do you connect this node-inspect to your app? It is really easy too. There is a couple of ways to do so, based on how your app is designed.
Option 1: Your application starts without problems.
If you want to debug an application and it is starts without a problem, then you can just do:
node --debug your-app.js
Next stepis to start node-inspector and attach it to NodeJS, on a different console (not necessary, but it makes the things easier), do the following:
Finally to start the debugging, open your browser and navigate yo tour server IP address or URL pointing to port 8080, if you are on your desktop (or laptop) it usually is something like http://localhost:8080.
Option 2: Your application dies at a very early stage OR it runs too fast to attach the debugger window.
If your app is dieing due to some bugs or it just runs and so fast that you can’t even start node-inspector, then you can use a different debug option that will pause the app as soon as it reaches the very first line of code
node --debug-brk your-app.js
And follow the steps to start the node-inspector from above. Once it is running and you are on your browser, you’ll see that the app code is there and it is already stopped on line 1. From there you can start debugging line by line, set breakpoints here and there… you know, the cool stuff that the developers (we) do.
Option 3: Special cases.
Last but not least, there are more complicated scenarios, like when you have a distributed app that runs splitting itself into workers thanks to the Cluster API, or maybe you cannot just stop the app and restart it, for this kind of scenarios you can also enable debug mode on your already running app by sending it a signal to start the debug process. The tricky part is to get your app (or worker) Process ID (PID), once you have it you can do the following (replace PID with your application’s Process ID):
kill -s USR1 PID
After doing so you can follow the same steps to start node-inspector.
Hope you find this as useful as I did. Remember, sharing is caring.