9

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 :(

thelolcat
  • 10,995
  • 21
  • 60
  • 102
  • just a suggestion, place a break point and inspect from your browser to view the contents of that object. – Ji_in_coding Jan 26 '15 at 17:05
  • 1
    but the node thing runs on a linux server. I can only send text to the browser. But as you can see I cannot convert it to json – thelolcat Jan 26 '15 at 17:07
  • 3
    http://stackoverflow.com/questions/10729276/node-js-console-log-object-content – epascarello Jan 26 '15 at 17:17
  • I prefer adding [this](https://stackoverflow.com/a/41882441/6243352) to the top of my script: `require("util").inspect.defaultOptions.depth = null;` – ggorlen May 05 '23 at 17:27

4 Answers4

14
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 })
Scimonster
  • 32,893
  • 9
  • 77
  • 89
3

Use the dir method.

console.dir(obj)
Oday Fraiwan
  • 1,147
  • 1
  • 9
  • 21
  • 1
    prints the same thing. I think this is meant for browsers only? – thelolcat Jan 26 '15 at 17:08
  • This method displays the content of the object as is. You can browse the whole object. What do you mean by "cutting" ? – Oday Fraiwan Jan 26 '15 at 17:11
  • the terminal thing only shows a few hundred lines, the rest get cut off – thelolcat Jan 26 '15 at 17:13
  • wow this is perfect thanks. I think most people use javascript in browser? – Andrew Mar 29 '17 at 12:49
  • interesting, the 50% of people the console.dir works other half doesn't. For me console.dir output is exactly the same like console.log. It chucks the object and print it as a text. I'd like to get them collapsed/expandible format – Peter Apr 22 '20 at 15:55
0

I've never used console.dir before, and that may very well be the correct way. However, what I've always done is just assign what I want to look at to a global variable, and then play with it directly inside the console.

Something like this:

var x;
function xyz(){
    // Code
    x = obj;
}

Then, directly in the console, if you type x, after xyz has run, you have your object there, and can play with the copy of it, including the ability to see all the properties, and modify it.

David
  • 4,744
  • 5
  • 33
  • 64
-3

You can use the dir method i believe

    console.dir(obj)

You're welcome ;)

Redlight
  • 17
  • 3