How to print objects in Node.js?

Sometimes, we want to print objects in Node.js.

In this article, we’ll look at how to print objects in Node.js.

How to print objects in Node.js?

To print objects in Node.js, we can use the util.inspect method.

For instance, we write:

const util = require('util')
const obj = {
  a: 0,
  b: 0
}
console.log(util.inspect(obj, {
  depth: null
}));

We call util.inspect with obj and { depth: null } to print the whole obj object.

Therefore, we see { a: 0, b: 0 } logged.

Conclusion

To print objects in Node.js, we can use the util.inspect method.