How can I get the full object in Node.js's console.log(), rather than '[Object]'?
When debugging using console.log()
, how can I get the full object?
const 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
.
javascript node.js debugging console.log
add a comment |
When debugging using console.log()
, how can I get the full object?
const 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
.
javascript node.js debugging console.log
add a comment |
When debugging using console.log()
, how can I get the full object?
const 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
.
javascript node.js debugging console.log
When debugging using console.log()
, how can I get the full object?
const 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
.
javascript node.js debugging console.log
javascript node.js debugging console.log
edited Aug 15 '16 at 9:51
Michał Perłakowski
45.1k16105122
45.1k16105122
asked May 23 '12 at 23:29
user1372449
add a comment |
add a comment |
13 Answers
13
active
oldest
votes
You need to use util.inspect()
:
const util = require('util')
console.log(util.inspect(myObject, {showHidden: false, depth: null}))
// 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.
4
Nice solution. Though no need to specify {showHidden: false} as long as it defaults to false.
– ecdeveloper
Dec 5 '14 at 11:29
24
Good to know; not sure when it was introduced, but as of at least nodev0.10.33
console.log()
implicitly appliesutil.inspect()
to its arguments, assuming the 1st one is not a format string. If you're happy withutil.inspect()
's default options, simplyconsole.log(myObject)
will do - no need to requireutil
;console.dir()
does the same, but accepts only ` object to inspect; as of at leastv0.11.14
, you can pass the options object forutil.inspect()
as the 2nd argument; my answer has more details.
– mklement0
Dec 17 '14 at 21:03
3
@mklement0 I have node v5.3.0 and when Iconsole.log(obj)
it still prints [Object] for deeply nested objects :( I really wish it would behave as you describe.
– SSH This
Feb 23 '16 at 22:36
24
@SSH:console.log()
is invariably limited to 2 levels (because it usesutil.inspect()
's default without allowing you to change it);console.dir()
has the same limit by default, but you can pass in an options object as the 2nd argument to change that (which is passed through toutil.inspect()
; note thatconsole.dir()
can only ever print 1 object at a time, however. To print with unlimited depth, useconsole.dir(myObject, { depth: null })
.
– mklement0
Feb 24 '16 at 0:02
3
console.dir(myObject, { depth: null })
is work for me
– Veck Hsiao
Apr 26 '17 at 5:08
|
show 5 more comments
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
2
Ah this is handy too! thanks!
– user1372449
May 23 '12 at 23:47
69
+1 for not needing to require anything
– Michael
Feb 23 '13 at 1:22
46
Note that you cannot JSON.stringify objects with circular references. Like it would occur with DOM objects, for example. Stringify will throw an "Error: Converting circular structure to JSON".
– Ignacio Lago
Jan 17 '14 at 16:47
11
keep in mind that this is not going to show undefined values
– vxsx
Feb 1 '15 at 12:37
8
this isn't the full object. objects containing only functions will be {}. Of course that may be a positive or a negative depending on what you want to print out.
– Larry W.
Dec 23 '15 at 5:57
|
show 7 more comments
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).
tl;dr
util.inspect()
is at the heart of diagnostic output: console.log()
and console.dir()
as well as the Node.js REPL use util.inspect()
implicitly, so it's generally not necessary to require('util')
and call util.inspect()
directly.
To get the desired output for the example in the question:
console.dir(myObject, { depth: null }); // `depth: null` ensures unlimited recursion
Details below.
console.log()
(and its alias,console.info()
):
If the 1st argument is NOT a format string: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 ]'
- Note that you cannot pass options through
util.inspect()
in this case, which implies 2 notable limitations:
- Structural depth of the output is limited to 2 levels (the default).
- Since you cannot change this with
console.log()
, you must instead useconsole.dir()
:console.dir(myObject, { depth: null }
prints with unlimited depth; see below.
- Since you cannot change this with
- You can't turn syntax coloring on.
- Structural depth of the output is limited to 2 levels (the default).
If the 1st argument IS a format string (see below): usesutil.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"}'
- Note:
- There is NO placeholder for representing objects
util.inspect()
-style. - JSON generated with
%j
is NOT pretty-printed.
- There is NO placeholder for representing objects
console.dir()
:
Accepts only 1 argument to inspect, and always appliesutil.inspect()
- essentially, a wrapper forutil.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.
node.js v0.11.14+: The optional 2nd argument specifies options forutil.inspect()
- see below; e.g.:
console.dir({ one: 1, two: 'deux'}, { colors: true }); // node 0.11+: Prints object representation with syntax coloring.
The REPL: implicitly prints any expression's return value withutil.inspect()
with syntax coloring;
i.e., just typing a variable's name and hitting Enter will print an inspected version of its value; e.g.:
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.
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 toInfinity
, 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:
Fails with objects that have circular references, such asmodule
in the global context.
Methods (functions) will by design NOT be included.- You can't opt into showing hidden (non-enumerable) properties.
- Example call:
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
- if
true
, then the object's non-enumerable properties [those designated not to show up when you usefor keys in obj
orObject.keys(obj)
] will be shown too. Defaults tofalse
.
- if
depth
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
null
.
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
colors
- if true, then the output will be styled with ANSI color codes. Defaults to
false
. Colors are customizable [... - see link].
- if true, then the output will be styled with ANSI color codes. Defaults to
customInspect
- if
false
, then custominspect()
functions defined on the objects being inspected won't be called. Defaults totrue
.
- if
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.
3
@YvesM. I'm glad you found something useful in it. The "messiness" stems from packing a lot of information into a concise answer, and bold-facing key parts is meant to make locating the important parts easier. How would you make it less messy without sacrificing these goals?
– mklement0
Apr 17 '18 at 15:30
3
@Ruudjah: bold-facing key parts is not "randomly making things bold" (although what the key parts are is a matter of taste, to a degree). Formatting code elements as such to my mind helps readability.
– mklement0
Apr 19 '18 at 23:30
1
Definitely the correct answer!
– Michael Czechowski
Oct 18 '18 at 8:33
add a comment |
Another simple method is to convert it to json
console.log('connection : %j', myObject);
9
Nice trick but the output won't be prettified, which makes it hard to read for large objects (the point of the question).
– Dan Dascalescu
Aug 1 '14 at 22:04
still very useful, and quicker to copy and paste into jsonlint.com than requiringutils
:)
– SSH This
Feb 23 '16 at 22:37
1
I think this one is great when you have an editor that will format json for you but you just need to copy it out from REPL
– jcollum
Dec 12 '17 at 21:07
This is very handy and helpful if the object is small
– Chinmay Samant
Nov 9 '18 at 6:43
add a comment |
Try this:
console.dir(myObject,{depth:null})
1
That's the SHORTEST solution!
– Martin Cup
Jan 21 at 17:50
add a comment |
You can also do
console.log(JSON.stringify(myObject, null, 3));
add a comment |
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.
1
console.dir is buggy and doesn't yet pass on theoptions
object toutil.inspect
.
– Dan Dascalescu
Aug 1 '14 at 22:47
As of (at least) v0.11.14, you can pass an options object as the 2nd argument, which is passed toutil.inspect()
.
– mklement0
Dec 17 '14 at 20:11
add a comment |
Since Node.js 6.4.0, this can be elegantly solved with util.inspect.defaultOptions
:
require("util").inspect.defaultOptions.depth = null;
console.log(myObject);
add a comment |
A good way to inspect objects is to use node --inspect option with Chrome DevTools for Node.
node.exe --inspect www.js
Open chrome://inspect/#devices
in chrome and click Open dedicated DevTools for Node
Now every logged object is available in inspector like regular JS running in chrome.
There is no need to reopen inspector, it connects to node automatically as soon as node starts or restarts. Both --inspect and Chrome DevTools for Node may not be available in older versions of Node and Chrome.
1
A message for me: try that out ->node.exe --inspect index.js
– Lonely
Mar 10 at 15:17
add a comment |
Both of these usages can be applied
// more compact and colour can be applied (better for process managers logging)
console.dir(queryArgs, { depth: null, colors: true });
// clear list of actual values
console.log(JSON.stringify(queryArgs, undefined, 2));
add a comment |
You can simply add an inspect()
method to your object which will override the representation of object in console.log
messages
eg:
var myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
myObject.inspect = function(){ return JSON.stringify( this, null, ' ' ); }
then, your object will be represented as required in both console.log and node shell
add a comment |
A simple trick would be use debug
module to add DEBUG_DEPTH=null
as environment variable when running the script
Ex.
DEBUG=* DEBUG_DEPTH=null node index.js
In you code
const debug = require('debug');
debug("%O", myObject);
Its giving error like "debug is not function"
– Bala
Nov 17 '17 at 2:23
@Bala You will need to install "debug" module in your project "npm install debug --save"
– Chintan
Nov 18 '17 at 8:17
add a comment |
The node REPL has a built-in solution for overriding how objects are displayed, see here.
The REPL module internally uses
util.inspect()
, when printing values.
However,util.inspect
delegates the call to the object'sinspect()
function, if it has one.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10729276%2fhow-can-i-get-the-full-object-in-node-jss-console-log-rather-than-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to use util.inspect()
:
const util = require('util')
console.log(util.inspect(myObject, {showHidden: false, depth: null}))
// 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.
4
Nice solution. Though no need to specify {showHidden: false} as long as it defaults to false.
– ecdeveloper
Dec 5 '14 at 11:29
24
Good to know; not sure when it was introduced, but as of at least nodev0.10.33
console.log()
implicitly appliesutil.inspect()
to its arguments, assuming the 1st one is not a format string. If you're happy withutil.inspect()
's default options, simplyconsole.log(myObject)
will do - no need to requireutil
;console.dir()
does the same, but accepts only ` object to inspect; as of at leastv0.11.14
, you can pass the options object forutil.inspect()
as the 2nd argument; my answer has more details.
– mklement0
Dec 17 '14 at 21:03
3
@mklement0 I have node v5.3.0 and when Iconsole.log(obj)
it still prints [Object] for deeply nested objects :( I really wish it would behave as you describe.
– SSH This
Feb 23 '16 at 22:36
24
@SSH:console.log()
is invariably limited to 2 levels (because it usesutil.inspect()
's default without allowing you to change it);console.dir()
has the same limit by default, but you can pass in an options object as the 2nd argument to change that (which is passed through toutil.inspect()
; note thatconsole.dir()
can only ever print 1 object at a time, however. To print with unlimited depth, useconsole.dir(myObject, { depth: null })
.
– mklement0
Feb 24 '16 at 0:02
3
console.dir(myObject, { depth: null })
is work for me
– Veck Hsiao
Apr 26 '17 at 5:08
|
show 5 more comments
You need to use util.inspect()
:
const util = require('util')
console.log(util.inspect(myObject, {showHidden: false, depth: null}))
// 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.
4
Nice solution. Though no need to specify {showHidden: false} as long as it defaults to false.
– ecdeveloper
Dec 5 '14 at 11:29
24
Good to know; not sure when it was introduced, but as of at least nodev0.10.33
console.log()
implicitly appliesutil.inspect()
to its arguments, assuming the 1st one is not a format string. If you're happy withutil.inspect()
's default options, simplyconsole.log(myObject)
will do - no need to requireutil
;console.dir()
does the same, but accepts only ` object to inspect; as of at leastv0.11.14
, you can pass the options object forutil.inspect()
as the 2nd argument; my answer has more details.
– mklement0
Dec 17 '14 at 21:03
3
@mklement0 I have node v5.3.0 and when Iconsole.log(obj)
it still prints [Object] for deeply nested objects :( I really wish it would behave as you describe.
– SSH This
Feb 23 '16 at 22:36
24
@SSH:console.log()
is invariably limited to 2 levels (because it usesutil.inspect()
's default without allowing you to change it);console.dir()
has the same limit by default, but you can pass in an options object as the 2nd argument to change that (which is passed through toutil.inspect()
; note thatconsole.dir()
can only ever print 1 object at a time, however. To print with unlimited depth, useconsole.dir(myObject, { depth: null })
.
– mklement0
Feb 24 '16 at 0:02
3
console.dir(myObject, { depth: null })
is work for me
– Veck Hsiao
Apr 26 '17 at 5:08
|
show 5 more comments
You need to use util.inspect()
:
const util = require('util')
console.log(util.inspect(myObject, {showHidden: false, depth: null}))
// 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 need to use util.inspect()
:
const util = require('util')
console.log(util.inspect(myObject, {showHidden: false, depth: null}))
// 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.
edited Sep 4 '18 at 18:28
Ikbel
3,93421935
3,93421935
answered May 23 '12 at 23:30
250R250R
23.4k62925
23.4k62925
4
Nice solution. Though no need to specify {showHidden: false} as long as it defaults to false.
– ecdeveloper
Dec 5 '14 at 11:29
24
Good to know; not sure when it was introduced, but as of at least nodev0.10.33
console.log()
implicitly appliesutil.inspect()
to its arguments, assuming the 1st one is not a format string. If you're happy withutil.inspect()
's default options, simplyconsole.log(myObject)
will do - no need to requireutil
;console.dir()
does the same, but accepts only ` object to inspect; as of at leastv0.11.14
, you can pass the options object forutil.inspect()
as the 2nd argument; my answer has more details.
– mklement0
Dec 17 '14 at 21:03
3
@mklement0 I have node v5.3.0 and when Iconsole.log(obj)
it still prints [Object] for deeply nested objects :( I really wish it would behave as you describe.
– SSH This
Feb 23 '16 at 22:36
24
@SSH:console.log()
is invariably limited to 2 levels (because it usesutil.inspect()
's default without allowing you to change it);console.dir()
has the same limit by default, but you can pass in an options object as the 2nd argument to change that (which is passed through toutil.inspect()
; note thatconsole.dir()
can only ever print 1 object at a time, however. To print with unlimited depth, useconsole.dir(myObject, { depth: null })
.
– mklement0
Feb 24 '16 at 0:02
3
console.dir(myObject, { depth: null })
is work for me
– Veck Hsiao
Apr 26 '17 at 5:08
|
show 5 more comments
4
Nice solution. Though no need to specify {showHidden: false} as long as it defaults to false.
– ecdeveloper
Dec 5 '14 at 11:29
24
Good to know; not sure when it was introduced, but as of at least nodev0.10.33
console.log()
implicitly appliesutil.inspect()
to its arguments, assuming the 1st one is not a format string. If you're happy withutil.inspect()
's default options, simplyconsole.log(myObject)
will do - no need to requireutil
;console.dir()
does the same, but accepts only ` object to inspect; as of at leastv0.11.14
, you can pass the options object forutil.inspect()
as the 2nd argument; my answer has more details.
– mklement0
Dec 17 '14 at 21:03
3
@mklement0 I have node v5.3.0 and when Iconsole.log(obj)
it still prints [Object] for deeply nested objects :( I really wish it would behave as you describe.
– SSH This
Feb 23 '16 at 22:36
24
@SSH:console.log()
is invariably limited to 2 levels (because it usesutil.inspect()
's default without allowing you to change it);console.dir()
has the same limit by default, but you can pass in an options object as the 2nd argument to change that (which is passed through toutil.inspect()
; note thatconsole.dir()
can only ever print 1 object at a time, however. To print with unlimited depth, useconsole.dir(myObject, { depth: null })
.
– mklement0
Feb 24 '16 at 0:02
3
console.dir(myObject, { depth: null })
is work for me
– Veck Hsiao
Apr 26 '17 at 5:08
4
4
Nice solution. Though no need to specify {showHidden: false} as long as it defaults to false.
– ecdeveloper
Dec 5 '14 at 11:29
Nice solution. Though no need to specify {showHidden: false} as long as it defaults to false.
– ecdeveloper
Dec 5 '14 at 11:29
24
24
Good to know; not sure when it was introduced, but as of at least node
v0.10.33
console.log()
implicitly applies util.inspect()
to its arguments, assuming the 1st one is not a format string. If you're happy with util.inspect()
's default options, simply console.log(myObject)
will do - no need to require util
; console.dir()
does the same, but accepts only ` object to inspect; as of at least v0.11.14
, you can pass the options object for util.inspect()
as the 2nd argument; my answer has more details.– mklement0
Dec 17 '14 at 21:03
Good to know; not sure when it was introduced, but as of at least node
v0.10.33
console.log()
implicitly applies util.inspect()
to its arguments, assuming the 1st one is not a format string. If you're happy with util.inspect()
's default options, simply console.log(myObject)
will do - no need to require util
; console.dir()
does the same, but accepts only ` object to inspect; as of at least v0.11.14
, you can pass the options object for util.inspect()
as the 2nd argument; my answer has more details.– mklement0
Dec 17 '14 at 21:03
3
3
@mklement0 I have node v5.3.0 and when I
console.log(obj)
it still prints [Object] for deeply nested objects :( I really wish it would behave as you describe.– SSH This
Feb 23 '16 at 22:36
@mklement0 I have node v5.3.0 and when I
console.log(obj)
it still prints [Object] for deeply nested objects :( I really wish it would behave as you describe.– SSH This
Feb 23 '16 at 22:36
24
24
@SSH:
console.log()
is invariably limited to 2 levels (because it uses util.inspect()
's default without allowing you to change it); console.dir()
has the same limit by default, but you can pass in an options object as the 2nd argument to change that (which is passed through to util.inspect()
; note that console.dir()
can only ever print 1 object at a time, however. To print with unlimited depth, use console.dir(myObject, { depth: null })
.– mklement0
Feb 24 '16 at 0:02
@SSH:
console.log()
is invariably limited to 2 levels (because it uses util.inspect()
's default without allowing you to change it); console.dir()
has the same limit by default, but you can pass in an options object as the 2nd argument to change that (which is passed through to util.inspect()
; note that console.dir()
can only ever print 1 object at a time, however. To print with unlimited depth, use console.dir(myObject, { depth: null })
.– mklement0
Feb 24 '16 at 0:02
3
3
console.dir(myObject, { depth: null })
is work for me– Veck Hsiao
Apr 26 '17 at 5:08
console.dir(myObject, { depth: null })
is work for me– Veck Hsiao
Apr 26 '17 at 5:08
|
show 5 more comments
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
2
Ah this is handy too! thanks!
– user1372449
May 23 '12 at 23:47
69
+1 for not needing to require anything
– Michael
Feb 23 '13 at 1:22
46
Note that you cannot JSON.stringify objects with circular references. Like it would occur with DOM objects, for example. Stringify will throw an "Error: Converting circular structure to JSON".
– Ignacio Lago
Jan 17 '14 at 16:47
11
keep in mind that this is not going to show undefined values
– vxsx
Feb 1 '15 at 12:37
8
this isn't the full object. objects containing only functions will be {}. Of course that may be a positive or a negative depending on what you want to print out.
– Larry W.
Dec 23 '15 at 5:57
|
show 7 more comments
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
2
Ah this is handy too! thanks!
– user1372449
May 23 '12 at 23:47
69
+1 for not needing to require anything
– Michael
Feb 23 '13 at 1:22
46
Note that you cannot JSON.stringify objects with circular references. Like it would occur with DOM objects, for example. Stringify will throw an "Error: Converting circular structure to JSON".
– Ignacio Lago
Jan 17 '14 at 16:47
11
keep in mind that this is not going to show undefined values
– vxsx
Feb 1 '15 at 12:37
8
this isn't the full object. objects containing only functions will be {}. Of course that may be a positive or a negative depending on what you want to print out.
– Larry W.
Dec 23 '15 at 5:57
|
show 7 more comments
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
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
edited Jul 7 '13 at 6:28
community wiki
2 revs, 2 users 92%
user1106925
2
Ah this is handy too! thanks!
– user1372449
May 23 '12 at 23:47
69
+1 for not needing to require anything
– Michael
Feb 23 '13 at 1:22
46
Note that you cannot JSON.stringify objects with circular references. Like it would occur with DOM objects, for example. Stringify will throw an "Error: Converting circular structure to JSON".
– Ignacio Lago
Jan 17 '14 at 16:47
11
keep in mind that this is not going to show undefined values
– vxsx
Feb 1 '15 at 12:37
8
this isn't the full object. objects containing only functions will be {}. Of course that may be a positive or a negative depending on what you want to print out.
– Larry W.
Dec 23 '15 at 5:57
|
show 7 more comments
2
Ah this is handy too! thanks!
– user1372449
May 23 '12 at 23:47
69
+1 for not needing to require anything
– Michael
Feb 23 '13 at 1:22
46
Note that you cannot JSON.stringify objects with circular references. Like it would occur with DOM objects, for example. Stringify will throw an "Error: Converting circular structure to JSON".
– Ignacio Lago
Jan 17 '14 at 16:47
11
keep in mind that this is not going to show undefined values
– vxsx
Feb 1 '15 at 12:37
8
this isn't the full object. objects containing only functions will be {}. Of course that may be a positive or a negative depending on what you want to print out.
– Larry W.
Dec 23 '15 at 5:57
2
2
Ah this is handy too! thanks!
– user1372449
May 23 '12 at 23:47
Ah this is handy too! thanks!
– user1372449
May 23 '12 at 23:47
69
69
+1 for not needing to require anything
– Michael
Feb 23 '13 at 1:22
+1 for not needing to require anything
– Michael
Feb 23 '13 at 1:22
46
46
Note that you cannot JSON.stringify objects with circular references. Like it would occur with DOM objects, for example. Stringify will throw an "Error: Converting circular structure to JSON".
– Ignacio Lago
Jan 17 '14 at 16:47
Note that you cannot JSON.stringify objects with circular references. Like it would occur with DOM objects, for example. Stringify will throw an "Error: Converting circular structure to JSON".
– Ignacio Lago
Jan 17 '14 at 16:47
11
11
keep in mind that this is not going to show undefined values
– vxsx
Feb 1 '15 at 12:37
keep in mind that this is not going to show undefined values
– vxsx
Feb 1 '15 at 12:37
8
8
this isn't the full object. objects containing only functions will be {}. Of course that may be a positive or a negative depending on what you want to print out.
– Larry W.
Dec 23 '15 at 5:57
this isn't the full object. objects containing only functions will be {}. Of course that may be a positive or a negative depending on what you want to print out.
– Larry W.
Dec 23 '15 at 5:57
|
show 7 more comments
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).
tl;dr
util.inspect()
is at the heart of diagnostic output: console.log()
and console.dir()
as well as the Node.js REPL use util.inspect()
implicitly, so it's generally not necessary to require('util')
and call util.inspect()
directly.
To get the desired output for the example in the question:
console.dir(myObject, { depth: null }); // `depth: null` ensures unlimited recursion
Details below.
console.log()
(and its alias,console.info()
):
If the 1st argument is NOT a format string: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 ]'
- Note that you cannot pass options through
util.inspect()
in this case, which implies 2 notable limitations:
- Structural depth of the output is limited to 2 levels (the default).
- Since you cannot change this with
console.log()
, you must instead useconsole.dir()
:console.dir(myObject, { depth: null }
prints with unlimited depth; see below.
- Since you cannot change this with
- You can't turn syntax coloring on.
- Structural depth of the output is limited to 2 levels (the default).
If the 1st argument IS a format string (see below): usesutil.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"}'
- Note:
- There is NO placeholder for representing objects
util.inspect()
-style. - JSON generated with
%j
is NOT pretty-printed.
- There is NO placeholder for representing objects
console.dir()
:
Accepts only 1 argument to inspect, and always appliesutil.inspect()
- essentially, a wrapper forutil.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.
node.js v0.11.14+: The optional 2nd argument specifies options forutil.inspect()
- see below; e.g.:
console.dir({ one: 1, two: 'deux'}, { colors: true }); // node 0.11+: Prints object representation with syntax coloring.
The REPL: implicitly prints any expression's return value withutil.inspect()
with syntax coloring;
i.e., just typing a variable's name and hitting Enter will print an inspected version of its value; e.g.:
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.
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 toInfinity
, 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:
Fails with objects that have circular references, such asmodule
in the global context.
Methods (functions) will by design NOT be included.- You can't opt into showing hidden (non-enumerable) properties.
- Example call:
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
- if
true
, then the object's non-enumerable properties [those designated not to show up when you usefor keys in obj
orObject.keys(obj)
] will be shown too. Defaults tofalse
.
- if
depth
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
null
.
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
colors
- if true, then the output will be styled with ANSI color codes. Defaults to
false
. Colors are customizable [... - see link].
- if true, then the output will be styled with ANSI color codes. Defaults to
customInspect
- if
false
, then custominspect()
functions defined on the objects being inspected won't be called. Defaults totrue
.
- if
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.
3
@YvesM. I'm glad you found something useful in it. The "messiness" stems from packing a lot of information into a concise answer, and bold-facing key parts is meant to make locating the important parts easier. How would you make it less messy without sacrificing these goals?
– mklement0
Apr 17 '18 at 15:30
3
@Ruudjah: bold-facing key parts is not "randomly making things bold" (although what the key parts are is a matter of taste, to a degree). Formatting code elements as such to my mind helps readability.
– mklement0
Apr 19 '18 at 23:30
1
Definitely the correct answer!
– Michael Czechowski
Oct 18 '18 at 8:33
add a comment |
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).
tl;dr
util.inspect()
is at the heart of diagnostic output: console.log()
and console.dir()
as well as the Node.js REPL use util.inspect()
implicitly, so it's generally not necessary to require('util')
and call util.inspect()
directly.
To get the desired output for the example in the question:
console.dir(myObject, { depth: null }); // `depth: null` ensures unlimited recursion
Details below.
console.log()
(and its alias,console.info()
):
If the 1st argument is NOT a format string: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 ]'
- Note that you cannot pass options through
util.inspect()
in this case, which implies 2 notable limitations:
- Structural depth of the output is limited to 2 levels (the default).
- Since you cannot change this with
console.log()
, you must instead useconsole.dir()
:console.dir(myObject, { depth: null }
prints with unlimited depth; see below.
- Since you cannot change this with
- You can't turn syntax coloring on.
- Structural depth of the output is limited to 2 levels (the default).
If the 1st argument IS a format string (see below): usesutil.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"}'
- Note:
- There is NO placeholder for representing objects
util.inspect()
-style. - JSON generated with
%j
is NOT pretty-printed.
- There is NO placeholder for representing objects
console.dir()
:
Accepts only 1 argument to inspect, and always appliesutil.inspect()
- essentially, a wrapper forutil.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.
node.js v0.11.14+: The optional 2nd argument specifies options forutil.inspect()
- see below; e.g.:
console.dir({ one: 1, two: 'deux'}, { colors: true }); // node 0.11+: Prints object representation with syntax coloring.
The REPL: implicitly prints any expression's return value withutil.inspect()
with syntax coloring;
i.e., just typing a variable's name and hitting Enter will print an inspected version of its value; e.g.:
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.
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 toInfinity
, 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:
Fails with objects that have circular references, such asmodule
in the global context.
Methods (functions) will by design NOT be included.- You can't opt into showing hidden (non-enumerable) properties.
- Example call:
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
- if
true
, then the object's non-enumerable properties [those designated not to show up when you usefor keys in obj
orObject.keys(obj)
] will be shown too. Defaults tofalse
.
- if
depth
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
null
.
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
colors
- if true, then the output will be styled with ANSI color codes. Defaults to
false
. Colors are customizable [... - see link].
- if true, then the output will be styled with ANSI color codes. Defaults to
customInspect
- if
false
, then custominspect()
functions defined on the objects being inspected won't be called. Defaults totrue
.
- if
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.
3
@YvesM. I'm glad you found something useful in it. The "messiness" stems from packing a lot of information into a concise answer, and bold-facing key parts is meant to make locating the important parts easier. How would you make it less messy without sacrificing these goals?
– mklement0
Apr 17 '18 at 15:30
3
@Ruudjah: bold-facing key parts is not "randomly making things bold" (although what the key parts are is a matter of taste, to a degree). Formatting code elements as such to my mind helps readability.
– mklement0
Apr 19 '18 at 23:30
1
Definitely the correct answer!
– Michael Czechowski
Oct 18 '18 at 8:33
add a comment |
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).
tl;dr
util.inspect()
is at the heart of diagnostic output: console.log()
and console.dir()
as well as the Node.js REPL use util.inspect()
implicitly, so it's generally not necessary to require('util')
and call util.inspect()
directly.
To get the desired output for the example in the question:
console.dir(myObject, { depth: null }); // `depth: null` ensures unlimited recursion
Details below.
console.log()
(and its alias,console.info()
):
If the 1st argument is NOT a format string: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 ]'
- Note that you cannot pass options through
util.inspect()
in this case, which implies 2 notable limitations:
- Structural depth of the output is limited to 2 levels (the default).
- Since you cannot change this with
console.log()
, you must instead useconsole.dir()
:console.dir(myObject, { depth: null }
prints with unlimited depth; see below.
- Since you cannot change this with
- You can't turn syntax coloring on.
- Structural depth of the output is limited to 2 levels (the default).
If the 1st argument IS a format string (see below): usesutil.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"}'
- Note:
- There is NO placeholder for representing objects
util.inspect()
-style. - JSON generated with
%j
is NOT pretty-printed.
- There is NO placeholder for representing objects
console.dir()
:
Accepts only 1 argument to inspect, and always appliesutil.inspect()
- essentially, a wrapper forutil.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.
node.js v0.11.14+: The optional 2nd argument specifies options forutil.inspect()
- see below; e.g.:
console.dir({ one: 1, two: 'deux'}, { colors: true }); // node 0.11+: Prints object representation with syntax coloring.
The REPL: implicitly prints any expression's return value withutil.inspect()
with syntax coloring;
i.e., just typing a variable's name and hitting Enter will print an inspected version of its value; e.g.:
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.
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 toInfinity
, 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:
Fails with objects that have circular references, such asmodule
in the global context.
Methods (functions) will by design NOT be included.- You can't opt into showing hidden (non-enumerable) properties.
- Example call:
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
- if
true
, then the object's non-enumerable properties [those designated not to show up when you usefor keys in obj
orObject.keys(obj)
] will be shown too. Defaults tofalse
.
- if
depth
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
null
.
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
colors
- if true, then the output will be styled with ANSI color codes. Defaults to
false
. Colors are customizable [... - see link].
- if true, then the output will be styled with ANSI color codes. Defaults to
customInspect
- if
false
, then custominspect()
functions defined on the objects being inspected won't be called. Defaults totrue
.
- if
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.
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).
tl;dr
util.inspect()
is at the heart of diagnostic output: console.log()
and console.dir()
as well as the Node.js REPL use util.inspect()
implicitly, so it's generally not necessary to require('util')
and call util.inspect()
directly.
To get the desired output for the example in the question:
console.dir(myObject, { depth: null }); // `depth: null` ensures unlimited recursion
Details below.
console.log()
(and its alias,console.info()
):
If the 1st argument is NOT a format string: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 ]'
- Note that you cannot pass options through
util.inspect()
in this case, which implies 2 notable limitations:
- Structural depth of the output is limited to 2 levels (the default).
- Since you cannot change this with
console.log()
, you must instead useconsole.dir()
:console.dir(myObject, { depth: null }
prints with unlimited depth; see below.
- Since you cannot change this with
- You can't turn syntax coloring on.
- Structural depth of the output is limited to 2 levels (the default).
If the 1st argument IS a format string (see below): usesutil.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"}'
- Note:
- There is NO placeholder for representing objects
util.inspect()
-style. - JSON generated with
%j
is NOT pretty-printed.
- There is NO placeholder for representing objects
console.dir()
:
Accepts only 1 argument to inspect, and always appliesutil.inspect()
- essentially, a wrapper forutil.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.
node.js v0.11.14+: The optional 2nd argument specifies options forutil.inspect()
- see below; e.g.:
console.dir({ one: 1, two: 'deux'}, { colors: true }); // node 0.11+: Prints object representation with syntax coloring.
The REPL: implicitly prints any expression's return value withutil.inspect()
with syntax coloring;
i.e., just typing a variable's name and hitting Enter will print an inspected version of its value; e.g.:
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.
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 toInfinity
, 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:
Fails with objects that have circular references, such asmodule
in the global context.
Methods (functions) will by design NOT be included.- You can't opt into showing hidden (non-enumerable) properties.
- Example call:
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
- if
true
, then the object's non-enumerable properties [those designated not to show up when you usefor keys in obj
orObject.keys(obj)
] will be shown too. Defaults tofalse
.
- if
depth
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
null
.
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
colors
- if true, then the output will be styled with ANSI color codes. Defaults to
false
. Colors are customizable [... - see link].
- if true, then the output will be styled with ANSI color codes. Defaults to
customInspect
- if
false
, then custominspect()
functions defined on the objects being inspected won't be called. Defaults totrue
.
- if
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.
edited Oct 8 '18 at 22:46
answered Dec 17 '14 at 20:57
mklement0mklement0
137k22255293
137k22255293
3
@YvesM. I'm glad you found something useful in it. The "messiness" stems from packing a lot of information into a concise answer, and bold-facing key parts is meant to make locating the important parts easier. How would you make it less messy without sacrificing these goals?
– mklement0
Apr 17 '18 at 15:30
3
@Ruudjah: bold-facing key parts is not "randomly making things bold" (although what the key parts are is a matter of taste, to a degree). Formatting code elements as such to my mind helps readability.
– mklement0
Apr 19 '18 at 23:30
1
Definitely the correct answer!
– Michael Czechowski
Oct 18 '18 at 8:33
add a comment |
3
@YvesM. I'm glad you found something useful in it. The "messiness" stems from packing a lot of information into a concise answer, and bold-facing key parts is meant to make locating the important parts easier. How would you make it less messy without sacrificing these goals?
– mklement0
Apr 17 '18 at 15:30
3
@Ruudjah: bold-facing key parts is not "randomly making things bold" (although what the key parts are is a matter of taste, to a degree). Formatting code elements as such to my mind helps readability.
– mklement0
Apr 19 '18 at 23:30
1
Definitely the correct answer!
– Michael Czechowski
Oct 18 '18 at 8:33
3
3
@YvesM. I'm glad you found something useful in it. The "messiness" stems from packing a lot of information into a concise answer, and bold-facing key parts is meant to make locating the important parts easier. How would you make it less messy without sacrificing these goals?
– mklement0
Apr 17 '18 at 15:30
@YvesM. I'm glad you found something useful in it. The "messiness" stems from packing a lot of information into a concise answer, and bold-facing key parts is meant to make locating the important parts easier. How would you make it less messy without sacrificing these goals?
– mklement0
Apr 17 '18 at 15:30
3
3
@Ruudjah: bold-facing key parts is not "randomly making things bold" (although what the key parts are is a matter of taste, to a degree). Formatting code elements as such to my mind helps readability.
– mklement0
Apr 19 '18 at 23:30
@Ruudjah: bold-facing key parts is not "randomly making things bold" (although what the key parts are is a matter of taste, to a degree). Formatting code elements as such to my mind helps readability.
– mklement0
Apr 19 '18 at 23:30
1
1
Definitely the correct answer!
– Michael Czechowski
Oct 18 '18 at 8:33
Definitely the correct answer!
– Michael Czechowski
Oct 18 '18 at 8:33
add a comment |
Another simple method is to convert it to json
console.log('connection : %j', myObject);
9
Nice trick but the output won't be prettified, which makes it hard to read for large objects (the point of the question).
– Dan Dascalescu
Aug 1 '14 at 22:04
still very useful, and quicker to copy and paste into jsonlint.com than requiringutils
:)
– SSH This
Feb 23 '16 at 22:37
1
I think this one is great when you have an editor that will format json for you but you just need to copy it out from REPL
– jcollum
Dec 12 '17 at 21:07
This is very handy and helpful if the object is small
– Chinmay Samant
Nov 9 '18 at 6:43
add a comment |
Another simple method is to convert it to json
console.log('connection : %j', myObject);
9
Nice trick but the output won't be prettified, which makes it hard to read for large objects (the point of the question).
– Dan Dascalescu
Aug 1 '14 at 22:04
still very useful, and quicker to copy and paste into jsonlint.com than requiringutils
:)
– SSH This
Feb 23 '16 at 22:37
1
I think this one is great when you have an editor that will format json for you but you just need to copy it out from REPL
– jcollum
Dec 12 '17 at 21:07
This is very handy and helpful if the object is small
– Chinmay Samant
Nov 9 '18 at 6:43
add a comment |
Another simple method is to convert it to json
console.log('connection : %j', myObject);
Another simple method is to convert it to json
console.log('connection : %j', myObject);
answered Jan 15 '14 at 16:05
niksmacniksmac
1,29431740
1,29431740
9
Nice trick but the output won't be prettified, which makes it hard to read for large objects (the point of the question).
– Dan Dascalescu
Aug 1 '14 at 22:04
still very useful, and quicker to copy and paste into jsonlint.com than requiringutils
:)
– SSH This
Feb 23 '16 at 22:37
1
I think this one is great when you have an editor that will format json for you but you just need to copy it out from REPL
– jcollum
Dec 12 '17 at 21:07
This is very handy and helpful if the object is small
– Chinmay Samant
Nov 9 '18 at 6:43
add a comment |
9
Nice trick but the output won't be prettified, which makes it hard to read for large objects (the point of the question).
– Dan Dascalescu
Aug 1 '14 at 22:04
still very useful, and quicker to copy and paste into jsonlint.com than requiringutils
:)
– SSH This
Feb 23 '16 at 22:37
1
I think this one is great when you have an editor that will format json for you but you just need to copy it out from REPL
– jcollum
Dec 12 '17 at 21:07
This is very handy and helpful if the object is small
– Chinmay Samant
Nov 9 '18 at 6:43
9
9
Nice trick but the output won't be prettified, which makes it hard to read for large objects (the point of the question).
– Dan Dascalescu
Aug 1 '14 at 22:04
Nice trick but the output won't be prettified, which makes it hard to read for large objects (the point of the question).
– Dan Dascalescu
Aug 1 '14 at 22:04
still very useful, and quicker to copy and paste into jsonlint.com than requiring
utils
:)– SSH This
Feb 23 '16 at 22:37
still very useful, and quicker to copy and paste into jsonlint.com than requiring
utils
:)– SSH This
Feb 23 '16 at 22:37
1
1
I think this one is great when you have an editor that will format json for you but you just need to copy it out from REPL
– jcollum
Dec 12 '17 at 21:07
I think this one is great when you have an editor that will format json for you but you just need to copy it out from REPL
– jcollum
Dec 12 '17 at 21:07
This is very handy and helpful if the object is small
– Chinmay Samant
Nov 9 '18 at 6:43
This is very handy and helpful if the object is small
– Chinmay Samant
Nov 9 '18 at 6:43
add a comment |
Try this:
console.dir(myObject,{depth:null})
1
That's the SHORTEST solution!
– Martin Cup
Jan 21 at 17:50
add a comment |
Try this:
console.dir(myObject,{depth:null})
1
That's the SHORTEST solution!
– Martin Cup
Jan 21 at 17:50
add a comment |
Try this:
console.dir(myObject,{depth:null})
Try this:
console.dir(myObject,{depth:null})
edited Oct 19 '15 at 12:12
MarmiK
4,56342841
4,56342841
answered Oct 19 '15 at 11:05
hirrahirra
535513
535513
1
That's the SHORTEST solution!
– Martin Cup
Jan 21 at 17:50
add a comment |
1
That's the SHORTEST solution!
– Martin Cup
Jan 21 at 17:50
1
1
That's the SHORTEST solution!
– Martin Cup
Jan 21 at 17:50
That's the SHORTEST solution!
– Martin Cup
Jan 21 at 17:50
add a comment |
You can also do
console.log(JSON.stringify(myObject, null, 3));
add a comment |
You can also do
console.log(JSON.stringify(myObject, null, 3));
add a comment |
You can also do
console.log(JSON.stringify(myObject, null, 3));
You can also do
console.log(JSON.stringify(myObject, null, 3));
edited Mar 9 '16 at 2:27
evandrix
4,81332330
4,81332330
answered Feb 14 '16 at 10:32
EesaEesa
1,00521737
1,00521737
add a comment |
add a comment |
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.
1
console.dir is buggy and doesn't yet pass on theoptions
object toutil.inspect
.
– Dan Dascalescu
Aug 1 '14 at 22:47
As of (at least) v0.11.14, you can pass an options object as the 2nd argument, which is passed toutil.inspect()
.
– mklement0
Dec 17 '14 at 20:11
add a comment |
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.
1
console.dir is buggy and doesn't yet pass on theoptions
object toutil.inspect
.
– Dan Dascalescu
Aug 1 '14 at 22:47
As of (at least) v0.11.14, you can pass an options object as the 2nd argument, which is passed toutil.inspect()
.
– mklement0
Dec 17 '14 at 20:11
add a comment |
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.
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.
answered Mar 2 '14 at 1:27
Luke WLuke W
4,03222625
4,03222625
1
console.dir is buggy and doesn't yet pass on theoptions
object toutil.inspect
.
– Dan Dascalescu
Aug 1 '14 at 22:47
As of (at least) v0.11.14, you can pass an options object as the 2nd argument, which is passed toutil.inspect()
.
– mklement0
Dec 17 '14 at 20:11
add a comment |
1
console.dir is buggy and doesn't yet pass on theoptions
object toutil.inspect
.
– Dan Dascalescu
Aug 1 '14 at 22:47
As of (at least) v0.11.14, you can pass an options object as the 2nd argument, which is passed toutil.inspect()
.
– mklement0
Dec 17 '14 at 20:11
1
1
console.dir is buggy and doesn't yet pass on the
options
object to util.inspect
.– Dan Dascalescu
Aug 1 '14 at 22:47
console.dir is buggy and doesn't yet pass on the
options
object to util.inspect
.– Dan Dascalescu
Aug 1 '14 at 22:47
As of (at least) v0.11.14, you can pass an options object as the 2nd argument, which is passed to
util.inspect()
.– mklement0
Dec 17 '14 at 20:11
As of (at least) v0.11.14, you can pass an options object as the 2nd argument, which is passed to
util.inspect()
.– mklement0
Dec 17 '14 at 20:11
add a comment |
Since Node.js 6.4.0, this can be elegantly solved with util.inspect.defaultOptions
:
require("util").inspect.defaultOptions.depth = null;
console.log(myObject);
add a comment |
Since Node.js 6.4.0, this can be elegantly solved with util.inspect.defaultOptions
:
require("util").inspect.defaultOptions.depth = null;
console.log(myObject);
add a comment |
Since Node.js 6.4.0, this can be elegantly solved with util.inspect.defaultOptions
:
require("util").inspect.defaultOptions.depth = null;
console.log(myObject);
Since Node.js 6.4.0, this can be elegantly solved with util.inspect.defaultOptions
:
require("util").inspect.defaultOptions.depth = null;
console.log(myObject);
answered Jan 26 '17 at 20:32
silverwindsilverwind
1,072917
1,072917
add a comment |
add a comment |
A good way to inspect objects is to use node --inspect option with Chrome DevTools for Node.
node.exe --inspect www.js
Open chrome://inspect/#devices
in chrome and click Open dedicated DevTools for Node
Now every logged object is available in inspector like regular JS running in chrome.
There is no need to reopen inspector, it connects to node automatically as soon as node starts or restarts. Both --inspect and Chrome DevTools for Node may not be available in older versions of Node and Chrome.
1
A message for me: try that out ->node.exe --inspect index.js
– Lonely
Mar 10 at 15:17
add a comment |
A good way to inspect objects is to use node --inspect option with Chrome DevTools for Node.
node.exe --inspect www.js
Open chrome://inspect/#devices
in chrome and click Open dedicated DevTools for Node
Now every logged object is available in inspector like regular JS running in chrome.
There is no need to reopen inspector, it connects to node automatically as soon as node starts or restarts. Both --inspect and Chrome DevTools for Node may not be available in older versions of Node and Chrome.
1
A message for me: try that out ->node.exe --inspect index.js
– Lonely
Mar 10 at 15:17
add a comment |
A good way to inspect objects is to use node --inspect option with Chrome DevTools for Node.
node.exe --inspect www.js
Open chrome://inspect/#devices
in chrome and click Open dedicated DevTools for Node
Now every logged object is available in inspector like regular JS running in chrome.
There is no need to reopen inspector, it connects to node automatically as soon as node starts or restarts. Both --inspect and Chrome DevTools for Node may not be available in older versions of Node and Chrome.
A good way to inspect objects is to use node --inspect option with Chrome DevTools for Node.
node.exe --inspect www.js
Open chrome://inspect/#devices
in chrome and click Open dedicated DevTools for Node
Now every logged object is available in inspector like regular JS running in chrome.
There is no need to reopen inspector, it connects to node automatically as soon as node starts or restarts. Both --inspect and Chrome DevTools for Node may not be available in older versions of Node and Chrome.
edited Apr 17 '17 at 11:43
answered Apr 17 '17 at 11:37
AliAli
12.1k106583
12.1k106583
1
A message for me: try that out ->node.exe --inspect index.js
– Lonely
Mar 10 at 15:17
add a comment |
1
A message for me: try that out ->node.exe --inspect index.js
– Lonely
Mar 10 at 15:17
1
1
A message for me: try that out ->
node.exe --inspect index.js
– Lonely
Mar 10 at 15:17
A message for me: try that out ->
node.exe --inspect index.js
– Lonely
Mar 10 at 15:17
add a comment |
Both of these usages can be applied
// more compact and colour can be applied (better for process managers logging)
console.dir(queryArgs, { depth: null, colors: true });
// clear list of actual values
console.log(JSON.stringify(queryArgs, undefined, 2));
add a comment |
Both of these usages can be applied
// more compact and colour can be applied (better for process managers logging)
console.dir(queryArgs, { depth: null, colors: true });
// clear list of actual values
console.log(JSON.stringify(queryArgs, undefined, 2));
add a comment |
Both of these usages can be applied
// more compact and colour can be applied (better for process managers logging)
console.dir(queryArgs, { depth: null, colors: true });
// clear list of actual values
console.log(JSON.stringify(queryArgs, undefined, 2));
Both of these usages can be applied
// more compact and colour can be applied (better for process managers logging)
console.dir(queryArgs, { depth: null, colors: true });
// clear list of actual values
console.log(JSON.stringify(queryArgs, undefined, 2));
edited Jan 11 '18 at 13:16
Dmitriy
4,996112034
4,996112034
answered Jan 11 '18 at 12:55
ErceErce
10618
10618
add a comment |
add a comment |
You can simply add an inspect()
method to your object which will override the representation of object in console.log
messages
eg:
var myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
myObject.inspect = function(){ return JSON.stringify( this, null, ' ' ); }
then, your object will be represented as required in both console.log and node shell
add a comment |
You can simply add an inspect()
method to your object which will override the representation of object in console.log
messages
eg:
var myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
myObject.inspect = function(){ return JSON.stringify( this, null, ' ' ); }
then, your object will be represented as required in both console.log and node shell
add a comment |
You can simply add an inspect()
method to your object which will override the representation of object in console.log
messages
eg:
var myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
myObject.inspect = function(){ return JSON.stringify( this, null, ' ' ); }
then, your object will be represented as required in both console.log and node shell
You can simply add an inspect()
method to your object which will override the representation of object in console.log
messages
eg:
var myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
myObject.inspect = function(){ return JSON.stringify( this, null, ' ' ); }
then, your object will be represented as required in both console.log and node shell
edited Nov 25 '15 at 6:27
T J
32.9k957112
32.9k957112
answered Sep 30 '15 at 12:47
harish2704harish2704
338310
338310
add a comment |
add a comment |
A simple trick would be use debug
module to add DEBUG_DEPTH=null
as environment variable when running the script
Ex.
DEBUG=* DEBUG_DEPTH=null node index.js
In you code
const debug = require('debug');
debug("%O", myObject);
Its giving error like "debug is not function"
– Bala
Nov 17 '17 at 2:23
@Bala You will need to install "debug" module in your project "npm install debug --save"
– Chintan
Nov 18 '17 at 8:17
add a comment |
A simple trick would be use debug
module to add DEBUG_DEPTH=null
as environment variable when running the script
Ex.
DEBUG=* DEBUG_DEPTH=null node index.js
In you code
const debug = require('debug');
debug("%O", myObject);
Its giving error like "debug is not function"
– Bala
Nov 17 '17 at 2:23
@Bala You will need to install "debug" module in your project "npm install debug --save"
– Chintan
Nov 18 '17 at 8:17
add a comment |
A simple trick would be use debug
module to add DEBUG_DEPTH=null
as environment variable when running the script
Ex.
DEBUG=* DEBUG_DEPTH=null node index.js
In you code
const debug = require('debug');
debug("%O", myObject);
A simple trick would be use debug
module to add DEBUG_DEPTH=null
as environment variable when running the script
Ex.
DEBUG=* DEBUG_DEPTH=null node index.js
In you code
const debug = require('debug');
debug("%O", myObject);
edited Nov 18 '17 at 8:18
answered Jul 27 '17 at 11:35
ChintanChintan
433414
433414
Its giving error like "debug is not function"
– Bala
Nov 17 '17 at 2:23
@Bala You will need to install "debug" module in your project "npm install debug --save"
– Chintan
Nov 18 '17 at 8:17
add a comment |
Its giving error like "debug is not function"
– Bala
Nov 17 '17 at 2:23
@Bala You will need to install "debug" module in your project "npm install debug --save"
– Chintan
Nov 18 '17 at 8:17
Its giving error like "debug is not function"
– Bala
Nov 17 '17 at 2:23
Its giving error like "debug is not function"
– Bala
Nov 17 '17 at 2:23
@Bala You will need to install "debug" module in your project "npm install debug --save"
– Chintan
Nov 18 '17 at 8:17
@Bala You will need to install "debug" module in your project "npm install debug --save"
– Chintan
Nov 18 '17 at 8:17
add a comment |
The node REPL has a built-in solution for overriding how objects are displayed, see here.
The REPL module internally uses
util.inspect()
, when printing values.
However,util.inspect
delegates the call to the object'sinspect()
function, if it has one.
add a comment |
The node REPL has a built-in solution for overriding how objects are displayed, see here.
The REPL module internally uses
util.inspect()
, when printing values.
However,util.inspect
delegates the call to the object'sinspect()
function, if it has one.
add a comment |
The node REPL has a built-in solution for overriding how objects are displayed, see here.
The REPL module internally uses
util.inspect()
, when printing values.
However,util.inspect
delegates the call to the object'sinspect()
function, if it has one.
The node REPL has a built-in solution for overriding how objects are displayed, see here.
The REPL module internally uses
util.inspect()
, when printing values.
However,util.inspect
delegates the call to the object'sinspect()
function, if it has one.
answered Feb 11 '16 at 17:02
LloydLloyd
6,2872646
6,2872646
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10729276%2fhow-can-i-get-the-full-object-in-node-jss-console-log-rather-than-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown