Node.js
Console.log
Object Handling
JavaScript
Coding Tips

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Node.js, when you work with complex or deeply nested objects and attempt to output them using console.log(), you might find that the representation is often truncated or simplified with placeholder texts like [Object]. This lack of detail can be frustrating when debugging or when you're trying to understand the structure and data of a large object. Fortunately, there are several approaches to output a full object in the console:

Understanding the Issue

The default behavior of console.log() in Node.js limits the depth it will print out an object's nested structures. This is primarily to avoid performance issues and to prevent the console from being overwhelmed by huge amounts of data from deeply nested objects. The depth limit for objects is usually set to 2.

Example:

javascript
1const obj = {
2  level1: {
3    level2: {
4      level3: {
5        data: "hidden"
6      }
7    }
8  }
9};
10
11console.log(obj);

The output might look like this:

 
{ level1: { level2: [Object] } }

As seen, the data inside level3 is not displayed and is instead represented as [Object].

Solutions to Fully Log Objects

1. Using util.inspect()

Node.js provides the util module, which includes a method called inspect for printing object representations. This method can be used to control the depth to which you would like to log an object.

javascript
const util = require('util');

console.log(util.inspect(obj, { showHidden: false, depth: null }));
  • showHidden: This is a boolean that shows non-enumerable symbols and properties of the object.
  • depth: This is an integer that tells util.inspect() how many times to recurse while formatting the object. Setting it to null means unlimited recursion.

2. Using JSON.stringify()

Another approach is to convert the object into a JSON string, which shows the whole object unless there are circular references.

javascript
console.log(JSON.stringify(obj, null, 2));
  • The second argument (null here) is a replacer function that could be used to modify the resulting output.
  • The third argument specifies the number of spaces to use as white space for beautification.

3. Custom Console Override

If you frequently need to log deep objects, you might consider overriding console.log() by wrapping it with a custom function employing either util.inspect() or JSON.stringify() as shown above.

javascript
1const originalConsoleLog = console.log;
2console.log = (obj) => {
3  originalConsoleLog(util.inspect(obj, { depth: null }));
4};

Considerations and Limitations

  • Performance: Note that logging vast and deeply nested objects can be CPU and memory-intensive, potentially slowing down your application.
  • Circular References: JSON.stringify() will throw an error if the object contains circular references (objects referencing themselves). util.inspect() handles circular references more gracefully.
  • Security: Be cautious of logging sensitive information. Logging highly detailed objects can inadvertently expose sensitive data.

Summary Table

MethodDepth ControlHandles Circular ReferencesSuitable for Debug Environment
console.log DefaultLimited (2)YesYes
util.inspect()CustomizableYesYes
JSON.stringify()UnlimitedNoConditional

Conclusion

To get the full object in Node.js's console.log(), you need to either utilize the util.inspect() method with appropriate settings to adjust depth and visibility or convert the object into a JSON string using JSON.stringify(). Each method has its own advantages and limitations, so the choice of method depends on the specific requirements and complexity of your data structures. Care should be taken regarding performance and security implications when logging detailed data.


Course illustration
Course illustration

All Rights Reserved.