Web App Development

Debugging Node.js with node-inspector

node-inspector allows you to use the Web Inspector Javascript debugger for node.js (you probably know Web Inspector from Google Chrome).



Installing node-inspector

Run sudo npm install -g node-inspector to install node-inspector. If it tells you that it couldn't install some "blazing fast native extensions" don't worry: it'll still work fine.

Using node-inspector

Now you'll need two terminal windows:
1) Run the program in debug mode: node --debug-brk test.js
2) Start the debugger: node-inspector
--debug-brk causes the node program to break immediately

Node-inspector will tell you to got to http://0.0.0.0:8080/debug?port=5858 to start debugging.
If it isn't loading properly: Try just using http://localhost:8080/, it did work that way for me.

--debug-brk and --debug options for node

When you use the --debug option you can set a breakpoint using debugger;. If your code finishes running before you can start node-inspector and go to the debugging page in the browser --debug-brk will set a breakpoint right at the beginning of the program.
Here's an example for using --debug:


setTimeout(function(){
var a = 5;
debugger;
a += 8;
}, 5000);

If you're getting 'connect ECONNREFUSED'

This probably means that you've tried to access the inspector in the browser while your node.js script isn't running. So first start your node process with --debug-brk and then run node-inspector.

Comments

Jony..
Thanks man!
That was exactly what i was looking for.

Follow me on Twitter