How I debug Node.js

Node.js is a server side runtime for Javascript apps. Being a server sided runtime, it doesn’t have a GUI. Therefore it’s abilities to provide an easy to use debugging interface are limited.

There are several ways of debugging Node.js, here are few of the reasons why I don’t like the most popular ones

Builtin CLI debugger

  1. It doesn’t have a GUI of course
  2. It’s slow and even hangs at times
  3. You have to remember it’s commands
  4. Debugging complex problems is nearly impossible if not impossible

node-inspector

  1. It shows ES6 Maps and Sets as null
  2. It shows objects as empty randomly
  3. It is generally slow
  4. It’s very unstable

IDE Debuggers

  1. The IDEs are costy
  2. Each has their own UI
  3. They are hard to setup
  4. They lack advance features

Electron to the rescue

Electron is an open source project by GitHub, it is basically Chromium + Node.js. It has the best of both worlds, node’s require, globals and all the other APIs along with Chromium Dev Tools.

I have written a small wrapper around Electron to allow for quick Node.js debugging. It’s called DeNode, short for Debug Node. You can install it using npm

npm install -g denode

It registers itself as denode bin, it accepts an optional file path of the node module to execute on boot.

denode
denode ./index
denode `which browserify`

What’s awesome about this?

  1. You can click and expand on deep nested objects
  2. You can profile your apps for memory leaks and CPU time
  3. You can set breakpoints on the fly
  4. You can update running code from dev tools
  5. You can compile your files with babel and debug real source instead of gibberish using sourcemaps
  6. Basically, all the awesomeness of Chromium Dev Tools

What’s the side effect?

  1. Not having the ability to execute it over a network or VM, theoretically you could do X forwarding but it would get too slow and painful

That’s what I use to debug my Node.js app, let me know what you think of it in the comments below.