user1529891
user1529891

Reputation:

Live debugging a nodejs app?

How can I debug a running nodejs app? I've found tools such as node-inspector, but it seems to only support starting the app and debugging from there.

Upvotes: 0

Views: 1308

Answers (1)

sobingt
sobingt

Reputation: 270

Debugging a running nodejs app.

This is the combination of a little documented feature of V8 mixed with a non-documented feature of the node.js debugger client. Say you have an already running node process that you want to debug.

# start the remote debugger in the already running node process
kill -s USR1 pid
# attach to the debugger using the node.js client
node debug host:5858
# see where you are
debug> pause
debug> bt

From there you can poke around. You can also continue and pause again to see if you seem to consistently end up in the same code area.

Debugging a nodejs app.

V8 comes with an extensive debugger which is accessible out-of-process via a simple TCP protocol. Node has a built-in client for this debugger. To use this, start Node with the debug argument; a prompt will appear:

% node debug myscript.js
< debugger listening on port 3000
connecting... ok
break in /home/username/Code/myscript.js:1
  1 x = 5;
  2 setTimeout(function () {
  3   debugger;
debug>
  • cont, c - Continue execution
  • next, n - Step next
  • step, s - Step in
  • out, o - Step out
  • pause - Pause running code

Check API for other commands reference and other details

You can also use node-inspector . Use it from any browser supporting websockets. Breakpoints, profiler, livecoding etc... It is really awesome.

Install it with

npm install -g node-inspector

then run

node-debug app.js

Upvotes: 4

Related Questions