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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github