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.