NodeUnit Debugging

In my previous post, I talked about debugging Node.js applications. We use nodeunit to unit test our Node.js scripts and it’s useful to be able to debug those nodeunit tests as well.

So how do you do that? It’s quite simple. You follow the instructions in my previous post except instead of pointing the debugger to a Node.js script, you point to your nodeunit instance and then you point to your test.

For example, if nodeunit is installed under node_modules and your test is under test folder, here’s how you’d debug that using node-inspector:

node --debug-brk node_modules/nodeunit/bin/nodeunit test/myDummyTest.t.js

Node.js debugging

In my current role at Skype, we do lots of prototyping to try out different ideas for various problems. We’ve been using Node.js heavily as it allows us to quickly iterate on different server architectures. Even though I’m not a big fan of JavaScript as a language (Why?), I do like Node.js as a prototyping platform. Once you get used to the callback mindset that comes with Node.js, you have pretty much everything you need to write some serious server side code.

In this post, I want to outline how to debug Node.js applications. I tried a few different debug tools for Node.js but I found node-inspector to be a decent tool to debug Node.js applications.

First, install node-inspector:

npm install -g node-inspector

Start your Node.js application with –debug-brk which tells the debugger to pause your script on the first line:

node --debug-brk yourNodeApp.js

Start node-inspector

node-inspector &

Go to Last time I tried, IE did not work, so try with a non-IE browser.

At this point, you should see your Node.js script stopped on the first line. Click on scripts tab to see all your scripts and set breakpoints where ever you want and then step through your code like you normally would.