This is the 10th in a series of posts leading up to Node.js Knockout on debugging node processes using Node Inspector. This post was written by Node Knockout judge and Node Inspector author Danny Coates.
While node has a built in debugger, Node Inspector provides a pleasant graphical interface for debugging node programs.
npm install -g node-inspector
Enable debug mode
To use node-inspector, enable debugging on the node you wish to debug. You can either start node with a debug flag like:
$ node --debug your/node/program.js
or, to pause your script on the first line:
$ node --debug-brk your/short/node/script.js
NOTE: make sure that the
--debug flag comes before
or else you may see an
Or you can enable debugging on a node that is already running by sending it a signal:
Get the PID of the node process using your favorite method.
ps -efare good
$ pgrep -l node
2345 node your/node/server.js
Send it the USR1 signal
$ kill -s USR1 2345
Great! Now you’re ready to attach node-inspector.
start the inspector. I usually put it in the background
$ node-inspector &
open http://127.0.0.1:8080/debug?port=5858 in your favorite WebKit based browser
select a script and set some breakpoints (far left line numbers) or simply add a
debuggercall in your code (node will break automatically on the call, just as V8 does).
then watch the slightly outdated but hilarious screencasts
node-inspector works almost exactly like the web inspector in Safari and
Chrome. Here’s a good overview of the UI.
I don’t see one of my script files in the file list.
try refreshing the browser (F5 or ⌘-r or control-r)
My script runs too fast to attach the debugger.
debuggerstatement in your code where you want the debugger to stop
--debug-brkto pause the script on the first line
Can I debug remotely?
Yes. node-inspector needs to run on the same machine as the node process, but your browser can be anywhere. Just make sure the firewall is open on 8080
I got the ui in a weird state.
when in doubt, refresh