How to console.log large object?

thelolcat picture thelolcat · Jan 26, 2015 · Viewed 12.6k times · Source

I'm trying to see what the object contains

With console.log(obj) the console cuts off from the lines and I can't see the entire structure.

Then I tried writing it to a file

fs.writeFile('test', JSON.stringify(obj));

But I get some error about circular references.

Is there any other way to view the object lol? The object is a "connection" from the nodejs websocket module. The docs are very poor and I can't seem to find the property that holds the IP address :(

Answer

Scimonster picture Scimonster · Jan 26, 2015
fs.writeFile('test', JSON.stringify(obj));

But I get some error about circular references.

That's what happens with JSON. JSON also ignores functions, regexes, dates, etc.

You can use util.inspect, which is what Node's console.log() uses internally to get the entire object, which can then be written to a file:

var util = require('util');
fs.writeFileSync('test.txt', util.inspect(obj));

And if you want infinite depth, and hidden properties:

util.inspect(obj, { showHidden: true, depth: null })