Debugging Node.js

I’ve never found the time to investigate better ways to debug Node.js than using console.log. So I’ve decided to do some googling and testing, and write-down here the result as a memo.

util.inspect

Just one little step better than using console.log, the util.inspect function generates a human-readable representations of the parameter specified. It’s like Ruby Object.inspect, but with some extra features.

I like using it like this:

// inspect the Boolean function constructor
// options: show hidden fields, 1 depth of recursion, use ANSI colors
util.puts(util.inspect(Boolean, true, 1, true));

In my terminal, this is the output:

A proper debugger

OK, let’s move to something more serious. Node.js has an easy to use built-debugger which hooks to the V8 JavaScript engine debugger. The best deal is using it via a package called node-inspector. It’s like a chain:

node-inspector (visual UI) -> Node.js debugger -> V8 debugger

This combination is really powerful. node-inspector uses the WebKit debugger as a UI. Here is a screenshot of me, inspecting an Express.js response object:

It’s also very easy to use. Just run node with --debug param, start node-inspector, and open a browser.

node-inspector can also be hooked to an existing process and can be easily used to do remote-debugging.

Resources

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s