Answer: Use console. log() or JSON. stringify() Method This method will print the object in browser console.
log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.
You need to use util.inspect()
:
const util = require('util')
console.log(util.inspect(myObject, {showHidden: false, depth: null, colors: true}))
// alternative shortcut
console.log(util.inspect(myObject, false, null, true /* enable colors */))
Outputs
{ a: 'a', b: { c: 'c', d: { e: 'e', f: { g: 'g', h: { i: 'i' } } } } }
See util.inspect()
docs.
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
A compilation of the many useful answers from (at least) Node.js v0.10.33
(stable) / v0.11.14
(unstable) presumably through (at least) v7.7.4
(the version current as of the latest update to this answer). Tip of the hat to Rory O'Kane for his help.
tl;dr
To get the desired output for the example in the question, use console.dir()
:
console.dir(myObject, { depth: null }); // `depth: null` ensures unlimited recursion
Why not util.inspect()
? Because it’s already at the heart of diagnostic output: console.log()
and console.dir()
as well as the Node.js REPL use util.inspect()
implicitly. It’s generally not necessary to require('util')
and call util.inspect()
directly.
Details below.
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:
console.log()
, you must instead use console.dir()
: console.dir(myObject, { depth: null }
prints with unlimited depth; see below.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(){} } // The REPL echoes the object definition with syntax coloring.
util.inspect()
automatically pretty-prints object and array representations, but produces multiline output only when needed.
The pretty-printing behavior can be controlled by the compact
property in the optional options
argument; false
uses multi-line output unconditionally, whereas true
disables pretty-printing altogether; it can also be set to a number (the default is 3
) to control the conditional multi-line behavior – see the docs.
By default, output is wrapped at around 60 characters thanks, Shrey , regardless of whether the output is sent to a file or a terminal. In practice, since line breaks only happen at property boundaries, you will often end up with shorter lines, but they can also be longer (e.g., with long property values).
In v6.3.0+ you can use the breakLength
option to override the 60-character limit; if you set it to Infinity
, everything is output on a single line.
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):
An optional options object may be passed that alters certain aspects of the formatted string; some of the properties supported are:
See the latest Node.js docs for the current, full list.
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)
Some of the supported placeholders are:
See the latest Node.js docs for the current, full list.
%s
– String.%d
– Number (both integer and float).%j
– JSON.%%
– single percent sign (‘%’). This does not consume an argument.Another simple method is to convert it to json
console.log('connection : %j', myObject);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With