When debugging using console.log, how can I get the full object?
var myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
console.log(myObject);
Outputs:
{ a: 'a', b: { c: 'c', d: { e: 'e', f: [Object] } } }
But i want to also see the content of property f
You need to use util.inspect()
var util = require('util');
console.log(util.inspect(myObject, {showHidden: false, depth: null}));
# alternative shortcut
console.log(util.inspect(myObject, false, null));
Outputs
{ a: 'a', b: { c: 'c', d: { e: 'e', f: { g: 'g', h: { i: 'i' } } } } }
See http://nodejs.org/api/util.html#util_util_inspect_object_options
You can use JSON.stringify
, and get some nice indentation as well as perhaps easier to remember syntax.
console.log(JSON.stringify(myObject, null, 4));
{
"a": "a",
"b": {
"c": "c",
"d": {
"e": "e",
"f": {
"g": "g",
"h": {
"i": "i"
}
}
}
}
}
The third argument sets the indentation level, so you can adjust that as desired.
More detail here if needed:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
Another simple method is to convert it to json
console.log('connection : %j', myObject);
A compilation of the many useful answers as of node.js v0.10.33
(stable) / v0.11.14
(unstable):
util.inspect()
is at the heart of diagnostic output - console.log()
and console.dir()
use it implicitly, as does the node.js REPL, so it's generally NOT necessary to require('util')
and call util.inspect()
directly:
console.log()
(and its alias, console.info()
):
util.inspect()
is automatically applied to every argument:
o = { one: 1, two: 'deux', foo: function(){} }; console.log(o, [1,2,3]) // -> '{ one: 1, two: 'deux', foo: [Function] } [ 1, 2, 3 ]'
util.inspect()
in this case, which implies 2 notable limitations:
util.format()
to print the remaining arguments based on the format string (see below); e.g.:
o = { one: 1, two: 'deux', foo: function(){} }; console.log('o as JSON: %j', o) // -> 'o as JSON: {"one":1,"two":"deux"}'
util.inspect()
-style.%j
is NOT pretty-printed.console.dir()
:
util.inspect()
- essentially, a wrapper for util.inspect()
without options by default; e.g.:
o = { one: 1, two: 'deux', foo: function(){} }; console.dir(o); // Effectively the same as console.log(o) in this case.
util.inspect()
- see below; e.g.:
console.dir({ one: 1, two: 'deux'}, { colors: true }); // node 0.11+: Prints object representation with syntax coloring.
util.inspect()
with syntax coloring;o = { one: 1, two: 'deux', foo: function(){} } // echoes the object definition with syntax coloring.
util.inspect()
automatically (and invariably) pretty-prints object and array representations, but produces multiline output only when needed - if everything fits on one line, only 1 line is printed.
If you want more control over pretty-printing, consider using JSON.stringify()
with a 3rd argument, but note the following:
module
in the global context.JSON.stringify({ one: 1, two: 'deux', three: true}, undefined, 2); // creates a pretty-printed multiline JSON representation indented with 2 spaces
util.inspect()
options object (2nd argument):
source: http://nodejs.org/api/util.html#util_util_format_format
An optional options object may be passed that alters certain aspects of the formatted string:
showHidden
true
, then the object's non-enumerable properties [those designated not to show up when you use for keys in obj
or Object.keys(obj)
] will be shown too. Defaults to false
.depth
null
.colors
false
. Colors are customizable [... - see link].customInspect
false
, then custom inspect()
functions defined on the objects being inspected won't be called. Defaults to true
.util.format()
format-string placeholders (1st argument)
source: http://nodejs.org/api/util.html#util_util_format_format
%s
- String.%d
- Number (both integer and float).%j
- JSON.%
- single percent sign ('%'). This does not consume an argument.perhaps console.dir
is all you need.
http://nodejs.org/api/console.html#console_console_dir_obj
Uses util.inspect on obj and prints resulting string to stdout.
use util option if you need more control.