Node.js: How to attach to a running process and to debug the server with a console?

I use 'forever' to run my application. I want to attach to the running environment to inspect my application. So what can I do?

From http://nodejs.org/api/debugger.html:

Advanced Usage

The V8 debugger can be enabled and accessed either by starting Node with the --debug command-line flag or by signaling an existing Node process with SIGUSR1.

Find the PID of your node process and then sending SIGUSR1 should do the trick:

kill -s SIGUSR1 nodejs-pid

Then run node-inspector and browse to the URL it indicates. More in this tutorial.

You can add a REPL to your app. For example, if you add a REPL to listen on localhost port 5001, you start your app as usual and login with telnet: telnet localhost 5001. That will take you to a prompt where you can interact with your app directly.

Alternatively, if you need to your app to "pause" when it reaches a certain state, you need to add "debugger;" lines to areas of your code where you want those breakpoints, then start the app in debug mode.

Hope that helps.

Even it's an old yet answered question, there is an easier way, which is passing parameters to node:

forever start -c 'node --debug-brk' main.js

If you don't want to wait for debugger to be attached, replace --debug-brk by --debug