Fortunately, it wasn’t long before the Node.js community came up with GUI debuggers and as of today, the most widespread one is node-inspector.
In this post, I won’t go into the details of using node-inspector but I will instead introduce a neat feature that many Node.js developers aren’t aware of or don’t know how to use.
This feature is called the
debugger statement. If you have ever caught
yourself firing up node-inspector and browsing through the huge file
pane on the left in order to find a file and insert a breakpoint, then
this post should save you a lot of trouble in the future.
Here’s what the ECMA-262 specification
has to say about the
Evaluating the DebuggerStatement production may allow an implementation to cause a breakpoint when run under a debugger. If a debugger is not present or active this statement has no observable effect.
In other words, you can insert breakpoints directly in your code using
debugger;, fire up your app in debug mode, open your debugging client
and the breakpoint will be set. Don’t forget to remove
your code once you are done debugging!
Here’s a quick demonstration.
Insert debugger; statement in your code
Fire up node-inspector
Launch your app in debugging mode (don’t forget to use the
--debug-brkflag if your debugger statement is “early” in your code)
node --debug server.js
Open the debugging client
No more hunting for files!