Explain Codes LogoExplain Codes Logo

How can I get the full object in Node.js's console.log(), rather than '

javascript
console-logging
object-inspection
debugging
Alex KataevbyAlex Kataev·Feb 10, 2025
TLDR

Swiftly display a comprehensive object in Node.js by harnessing the power of util.inspect. Take note of the depth option. Here's a juicy example:

const util = require('util'); console.log(util.inspect(yourObject, { showHidden: false, depth: null }));

Expanded Guide for Object Inspection

Being a master debugger requires insight into the microcosm of objects. An interwoven collection of keys and values that's a work of art. And behold, our tools for dissection.

Painting Logs with Colors

Who said debugging was dreary? Add a colorful touch to your session. Just set the colors option to true:

console.log(util.inspect(yourObject, { colors: true, depth: null })); // Picasso would be proud

Deep Diving with JSON.stringify

In the vast ocean of JSON objects, JSON.stringify is your submarine. Note, functions and circular references are like the Mariana Trench - unreachable. It's perfect for our nemo-esque objects:

console.log(JSON.stringify(yourObject, null, 4)); // 4 - the amount of oxygen tanks needed for this dive

Set it and Forget it

Why juggle the depth every time? Impress your audience with this magic trick:

util.inspect.defaultOptions.depth = null; // Magic! *Waves wand*

Handling Giant Objects

Objects can grow big like Jack's beanstalk. Here's a solution to prevent information overload:

console.log(util.inspect(yourObject, { compact: true, breakLength: 80 })); // because 80 is the new 20!

Readability matters

A well-indented JSON.stringify output is your secret recipe to clean and readable code:

console.log(JSON.stringify(yourObject, null, 4)); // Like a book, but with braces

Quick Snapshots with "%j"

Need a quick log snapshot? Deploy the %j and console.log. A perfect duo for a quick debrief:

console.log('Object: %j', yourObject); // Insta-worthy!

Journey into console.dir

console.dir is a quiet but powerful ally in object introspection. Use it wisely.

Descriptive Debugging

Capture logs in full technicolor detail of Node's V8 engine format by using console.dir:

console.dir(yourObject, { depth: null }); // Who needs 3D glasses?

The Hidden Treasures

ShowHidden: true opens up the secret vault of non-enumerable properties, revealing the hidden treasures of your object:

console.log(util.inspect(yourObject, { showHidden: true, depth: 2 })); // Argh, matey! Hidden treasures abound!

Note the complexity

Complex is not synonymous to confusing. Break down complex structures by manipulating the depth to adjust the level of detail:

console.log(util.inspect(yourObject, { depth: 2 })); // Like a 2-step skincare routine: simple & effective.

Customized Inspection Déjà vu

Create a customized roadmap to traverse through each object. Special objects, special treatment.

Rolling out the red carpet

If an object has a custom inspection function, roll out the red carpet and let util.inspect do the honours:

yourObject[util.inspect.custom] = function(depth) { return `Red Carpet View: {...}`; };

The global ensemble

Need a consistent inspection rhythm throughout your application? Set the tempo using global defaults:

require('util').inspect.defaultOptions.customInspect = false; // One rhythm to rule them all.