How I debug Node.js

April 15, 2016

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.

Update: Since this was written --inspect support has landed in Node.js. It's easier to use and doesn't require installing thirdparty tools. You can find more about it in Node.js debugging Docs.

Why I don't like Popular Options

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

Profile picture

Written by Anees Iqbal who likes writing code and racing cars. @steelbrain on Github / @realsteelbrain on Twitter / X.

© 2023, all code on this blog is licensed under MIT License. An RSS Feed is available.