193

Is it possible to print an objects contents e.g. methods and attributes in Node.js?

At the moment I'm trying to print the session object and get the following:

console.log("Session:" + session);
> Session:[object Object]

Maybe in a similar way to print_r(array) in PHP, or using .toString in Java.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Jack
  • 14,939
  • 19
  • 64
  • 92

8 Answers8

301

Try this one:

console.log("Session: %j", session);

If the object could be converted into JSON, that will work.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Alexander Sulfrian
  • 3,503
  • 1
  • 14
  • 9
149
function prettyJSON(obj) {
    console.log(JSON.stringify(obj, null, 2));
}

// obj -> value to convert to a JSON string
// null -> (do nothing)
// 2 -> 2 spaces per indent level

JSON.stringify on MDN

Jake Berger
  • 5,027
  • 1
  • 27
  • 22
ccgillett
  • 4,401
  • 4
  • 19
  • 14
33

To have an output more similar to the raw console.log(obj) I usually do use console.log('Status: ' + util.inspect(obj)) (JSON is slightly different).

lapo
  • 3,017
  • 24
  • 33
  • 4
    This gives "util is not defined". You must `var util = require("util") first. Also, the sub-objects still turn out as [Object] and not the JSON string that represents them. – juanpaco May 01 '13 at 12:26
  • 14
    To remove the depth limit, use: `require('util').inspect(obj, {depth:null})` – lapo May 02 '13 at 10:57
29

This will work with any object:

    var util = require("util");
    console.log(util.inspect(myObject, {showHidden: false, depth: null}));
Marwen Trabelsi
  • 4,071
  • 8
  • 38
  • 76
  • 2
    This works great for error objects. None of their properties show up in the other solutions. – Chris Jun 22 '15 at 21:14
  • 1
    This just hangs for me. I think circular references in an object crashes it – Richard May 19 '16 at 15:25
  • @Richard it probably hang(ed) for you due to the depth option being set to `null`. Inspect has built-in treatment for circular references. – kbtz Aug 15 '17 at 21:16
7

console.dir() is the most direct way.

rainabba
  • 3,425
  • 33
  • 32
  • console.dir(someJsonObject); still leaves nested objects unprinted, for me. – nyarasha Nov 10 '15 at 17:28
  • 1
    I think it won't drill into objects through their prototype and it won't deal with certain recursion. You're either going to have to do something like console.log(JSON.stringify()) or something more specific to your object if .dir() isn't sufficient. If you have more info about the object, perhaps more specific advice can be given. – rainabba Nov 11 '15 at 20:43
  • It can take the same options as util.inspect. Add {depth:null} – nikc.org Feb 26 '17 at 09:50
0
console.log(obj);

Run: node app.js > output.txt

0

This will for most of the objects for outputting in nodejs console

var util = require('util')
function print (data){
  console.log(util.inspect(data,true,12,true))
  
}

print({name : "Your name" ,age : "Your age"})
0

console.dir with the depth argument set will do the trick. This will print JSON objects to any arbitrary depth. Depth:null means print every level.

console.dir(someObject, { depth: null })
john ktejik
  • 5,576
  • 4
  • 45
  • 52