199

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,738
  • 21
  • 105
  • 131
Jack
  • 15,614
  • 19
  • 67
  • 92

9 Answers9

304

Try this one:

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alexander Sulfrian
  • 3,533
  • 1
  • 16
  • 9
  • 9
    I am trying to do this for the request object but it shows error TypeError: Converting circular structure to JSON Is there any way to limit the depth – Neil May 17 '13 at 10:29
  • anyway to pretty print this? – chovy Aug 13 '13 at 06:24
  • 6
    @chovy: something like this might work: console.log(JSON.stringify(json, null, 4)); – Eric Brandel Sep 03 '13 at 21:05
  • 1
    this worked for me: `console.log("Session: %O", session);` https://developer.mozilla.org/en-US/docs/Web/API/console#Outputting_text_to_the_console – JP Lew Jun 12 '19 at 22:02
  • this this console.log no fuss drop-in replacement https://www.npmjs.com/package/console-log-json – 7wp May 29 '23 at 20:07
154
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,237
  • 1
  • 28
  • 22
ccgillett
  • 4,511
  • 4
  • 21
  • 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,136
  • 26
  • 34
  • 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
30

This will work with any object:

    var util = require("util");
    console.log(util.inspect(myObject, {showHidden: false, depth: null}));
Marwen Trabelsi
  • 4,167
  • 8
  • 39
  • 80
  • 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. – cvsguimaraes Aug 15 '17 at 21:16
7

console.dir() is the most direct way.

rainabba
  • 3,804
  • 35
  • 35
  • 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
  • I like the formatting (a nice compromise between readability and compactness) but be warned that this does not output valid JSON: * single quotes around strings * object keys are not quoted if they're words – bart Mar 11 '23 at 12:10
2

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 k
  • 6,268
  • 4
  • 55
  • 59
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

If you are still looking for some options, there is a NPM package that might help you, a drop-in replacement for console.log called console-log-json. The nice thing about it is that it will create consistent JSON formatted logs, despite the fact that you can throw at console.log() any number of parameters in any order, including other JSON objects as well as Error objects and plain strings.

It also handles automatically adding some extra helpful attributes such as time stamps and file where the console.log() was called from, etc..

example JSON output

These all get printed to STDOUT so any log shipping and aggregator can pick it up and parse the JSON.

7wp
  • 12,505
  • 20
  • 77
  • 103