Debugging Node.js with pp (pretty-print)
Rubyist often use the handy global pp method to do quick console debugging. It’s easy to do the same in Node.js
Console-based debugging will never substitute a real debugger, but, most of the time, is more than enough to find and fix problems.
As a long-time rubyist, I’m used to debugging my code using IRB or Pry. Inside IRB or Pry I use the Ruby kernel pp
method to pretty-print objects. It’s quite handy it’s global. I use it all the time.
In Node, we have the console
object. Its log
, debug
, error
, and warn
methods dump objects as human-friendly as possible. However, when dumping complex objects you don’t have too much control.
Node also provides a nifty util
package with an inspect
method that lets you stringify any object and fine-tune the output.
I like to combine both tools and make my own global pp
method.
You can choose your preferred shared options, and later, override them as needed each time you call pp
.
This is just a quick tip, but it’s worth mentioning. You get so much with less effort 😍.