How can I get the full object in Node.js console.log () and not in [Object]?

When debugging using console.log(), how can I get the complete 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 also want to see the contents of the property f.

+758
source share
17 answers

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 documents . util.inspect()

+1271
source

You can use JSON.stringifyas well as get a good indentation, and it may also be easier to remember the 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 it as desired.

More details here, if necessary:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

+568

( ) Node.js v0.10.33 ()/v0.11.14 () ( ) v7.7.4 (, ) ,

;

util.inspect() : console.log() console.dir() REPL- util.inspect() Node.js util.inspect() util.inspect(), require('util') util.inspect() .

:

console.dir(myObject, { depth: null }); // 'depth: null' ensures unlimited recursion

.


  • console.log() ( console.info()):

    • 1- : util.inspect() :
      • o = { one: 1, two: 'deux', foo: function(){} }; console.log(o, [1,2,3])/ / -> '{ one: 1, two: 'deux', foo: [Function] } [ 1, 2, 3 ]'
      • , util.inspect() , 2 :
        • 2 ( ).
          • console.log(), console.dir(): console.dir(myObject, { depth: null } ; . .
        • .
    • 1- (. ): util.format() (. ); :
      • o = { one: 1, two: 'deux', foo: function(){} }; console.log('o as JSON: %j', o)/ / -> 'o as JSON: {"one":1,"two":"deux"}'
      • :
        • util.inspect() -style.
        • JSON, %j, .
  • console.dir():

    • 1 util.inspect() - , util.inspect() ; :
      • 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+: util.inspect() - . ; :
      • console.dir({ one: 1, two: 'deux'}, { colors: true });//node 0.11+: Prints object representation with syntax coloring.
  • REPL: util.inspect() ;
    , Enter, ; :
    • o = { one: 1, two: 'deux', foo: function(){} }//echoes the object definition with syntax coloring.

util.inspect() ( ) , - , 1 .

  • , 60 , . , , , (, ).

  • 6.3. 0+ breakLength 60 ; Infinity, .

, JSON.stringify() , :

  • , , module .
  • () .
  • ( ) .
  • :
    • JSON.stringify({ one: 1, two: 'deux', three: true}, undefined, 2);//creates a pretty-printed multiline JSON representation indented with 2 spaces

util.inspect() (2- ):

: http://nodejs.org/api/util.html#util_util_format_format

, :

  • showHidden
    • true, [, for keys in obj Object.keys(obj) ]. false.
  • depth
    • , . . 2. , null.
  • colors
    • true, ANSI. false. [... - . ].
  • customInspect
    • false, inspect() , . true.

util.format() (1- )

: http://nodejs.org/api/util.html#util_util_format_format

  • %s - .
  • %d - ( , ).
  • %j - JSON.
  • % - ('%'). .
+254

- json

console.log('connection : %j', myObject);
+51

:

console.dir(myObject,{depth:null})
+37

Node.js 6.4.0, util.inspect.defaultOptions:

require("util").inspect.defaultOptions.depth = null;
console.log(myObject);
+25

console.dir - , .

http://nodejs.org/api/console.html#console_console_dir_obj

util.inspect obj stdout.

util, .

+21

console.log(JSON.stringify(myObject, null, 3));
+18

node - Chrome DevTools Node.

node.exe --inspect www.js

chrome://inspect/#devices DevTools Node

, JS, .

enter image description here

, node, node . - Chrome DevTools Node node Chrome.

+14

// 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));
+10

inspect() , console.log

:

var myObject = {
   "a":"a",
   "b":{
      "c":"c",
      "d":{
         "e":"e",
         "f":{
            "g":"g",
            "h":{
               "i":"i"
            }
         }
      }
   }
};
myObject.inspect = function(){ return JSON.stringify( this, null, ' ' ); }

console.log, node shell

+4

, .

const myObject = {
   "a":"a",
   "b":{
      "c":"c",
      "d":{
         "e":"e",
         "f":{
            "g":"g",
            "h":{
               "i":"i"
            }
         }
      }
   }
};

console.log(JSON.stringify(myObject, null, '\t'));
Hide result

:

JSON.stringify . ( ).

+4

debug DEBUG_DEPTH=null script

.

DEBUG = * DEBUG_DEPTH = null node index.js

const debug = require('debug');
debug("%O", myObject);
+3

node REPL , . .

REPL util.inspect() . util.inspect inspect(), .

+2

This can print the key of the object and the value of the object in the simplest way. Just give it a try.

const jsonObj = {
  a: 'somestring',
  b: 42,
  c: false
};

Array.from(Object.keys(jsonObj)).forEach(function(key){
  console.log(key + ":" + jsonObj[key]);
});
0
source

The easiest option:

    console.log('%O', myObject);
Run codeHide result

0
source

Try this helper function

const l = (...params) => console.log(...params.map(param => JSON.stringify(param, null, 4)))

Using:

l(obj1,obj2...)

You can also use it in other javascript environments like PhantomJs , for example

-1
source

All Articles