201

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.

2
  • 9
    console.log("Session:" + util.inspect(session)) Commented Sep 15, 2011 at 12:15
  • 1
    For a more intuitive and visual output of objects take a look at nodedump: github.com/ragamufin/nodedump Commented Oct 3, 2013 at 23:41

9 Answers 9

306

Try this one:

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

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

Sign up to request clarification or add additional context in comments.

5 Comments

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
anyway to pretty print this?
@chovy: something like this might work: console.log(JSON.stringify(json, null, 4));
this worked for me: console.log("Session: %O", session); developer.mozilla.org/en-US/docs/Web/API/…
this this console.log no fuss drop-in replacement npmjs.com/package/console-log-json
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

Comments

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).

2 Comments

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.
To remove the depth limit, use: require('util').inspect(obj, {depth:null})
30

This will work with any object:

    var util = require("util");
    console.log(util.inspect(myObject, {showHidden: false, depth: null}));

3 Comments

This works great for error objects. None of their properties show up in the other solutions.
This just hangs for me. I think circular references in an object crashes it
@Richard it probably hang(ed) for you due to the depth option being set to null. Inspect has built-in treatment for circular references.
7

console.dir() is the most direct way.

4 Comments

console.dir(someJsonObject); still leaves nested objects unprinted, for me.
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.
It can take the same options as util.inspect. Add {depth:null}
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
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 })

Comments

0
console.log(obj);

Run: node app.js > output.txt

Comments

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"})

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.