27 Comments

Comparing Node.js Debug Options

I’ve been doing a lot of Node.js development lately. I wanted to get a better understanding of the debug options available for Node apps, so I thought I would do some investigation and summarize what I found.

Option 1: The Built-In Debugger

If you’ve done any Node development, you’re probably familiar with the built-in Node debugger. It supports placing debugger statements in your source code. When you run your app in debug mode via node debug [your app], it will break when a debugger statement is hit.

As an example, consider the following simple Node app:


for(var i=0; i<5; i++){
  debugger;
  console.log(i);
}
console.log("done");

If we run this app via node debug app.js,it will break when the code hits the debugger line (shown below).

break in app.js:3
  1
  2 for(var i=0; i<5; i++){
> 3   debugger;
  4   console.log(i);
  5 }

From here, we can use the following debug commands (found in the Node debugger docs) to step through our code :

continue – cont, c
step – next, n
step in – step, s
step out – out, o

At any point, we can open a REPL by typing repl to inspect or reassign variable values.

In my experience, I’ve found the REPL to be a bit slow at times. It occasionally hangs when inspecting especially large variables.

Option 2: Node Inspector

The built-in debugger works fine in some situations, but it takes a far back seat to Node Inspector. Node Inspector allows you to debug your app using a nice GUI debug interface inside of a browser. It currently only works in Chrome and Opera. If you’re familiar with the Chrome Developer Tools, Node Inspector will look familiar to you–the interface is identical.

Let’s look at an example. Consider the same simple Node app that we discussed above, but without the debugger command inserted into the source code.


for(var i=0; i<5; i++){
  console.log(i);
}
console.log("done");

To use Node Inspector to debug, you’ll need to install it from NPM via:


npm install -g node-inspector

Once installed, we can use it to run our Node app by typing:


node-debug app.js

You should see output that looks something like this:

Node Inspector is now available from http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858
Debugging `app.js`

Debugger listening on port 5858

It may open up Chrome (assuming it’s set as your default browser) and immediately bring up your app. If it doesn’t do that, simply navigate to the address from the console output (http://127.0.0.1:8080/?ws=127.0.0.1:8080&port=5858 in our example). Doing so results in a nice visual debug environment.

node_inspector_ss

You can browse your source code and set breakpoints just as you would when debugging client-side JavaScript.

Option 3: IDE Debuggers

Another way to get access to a nice Node debug environment is to use an IDE, such as Web Storm. The Web Storm integrated debugger is excellent. After setting up your project in Web Storm, you can just run it and debug as you would with any other IDE. I’ve included a sample screenshot below.

webstorm_debug_ss

That sums up my investigation. What’s your favorite method for debugging Node apps?