How to log a node object with circular references to the console

Darragh O'Riordan - Feb 1 '21 - - Dev Community

If you try to use JSON.stringify() on a NodeJS object you will get an error “Converting circular structure to JSON”. This is because NodeJS objects have circular references.

The way to stringify NodeJS objects is to use util.inspect().

util.inspect()

There is excellent documentation on the NodeJS site here.

But you can use it simply by just passing the object to the method without options.

util.inspect({ a: 1, b: 'b' })
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player