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

Better Stack Team
Updated on March 11, 2024

By default, console.log() in Node.js will display [Object] when trying to log an object. If you want to see the full contents of an object, you can use util.inspect() from the util module, which is built into Node.js.

Here's an example of how you can use util.inspect() to log the full object:

 
const util = require('util');

const myObject = {
  key1: 'value1',
  key2: 'value2',
  key3: {
    nestedKey: 'nestedValue'
  }
};

// Log the full object
console.log(util.inspect(myObject, { depth: null }));

In this example, util.inspect() is used to inspect the myObject. The second argument is an options object where { depth: null } means that the inspection should not be limited by depth. This is useful for deeply nested objects.

Alternatively, you can use util.inspect() directly within console.log() without importing the entire util module:

 
console.log(require('util').inspect(myObject, { depth: null }));

This approach allows you to inspect and log the full contents of an object, including nested structures, arrays, and other complex data types. Adjust the depth option according to your needs, and setting it to null will show the entire structure regardless of its depth.