How can I get the full object in Node.js's console.log(), rather than '[Object]'?

前端 未结 17 2862
一向
一向 2020-11-22 02:59

When debugging using console.log(), how can I get the full object?

const myObject = {
   \"a\":\"a\",
   \"b\":{
      \"c\":\"c\",
      \"d\":         


        
相关标签:
17条回答
  • 2020-11-22 03:06

    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.

    0 讨论(0)
  • 2020-11-22 03:07

    Easiest option:

        console.log('%O', myObject);

    0 讨论(0)
  • 2020-11-22 03:09

    Since Node.js 6.4.0, this can be elegantly solved with util.inspect.defaultOptions:

    require("util").inspect.defaultOptions.depth = null;
    console.log(myObject);
    
    0 讨论(0)
  • 2020-11-22 03:10

    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.

    0 讨论(0)
  • 2020-11-22 03:12

    Try this:

    console.dir(myObject,{depth:null})
    
    0 讨论(0)
  • 2020-11-22 03:16

    If you're looking for a way to show the hidden items in you array, you got to pass maxArrayLength: Infinity

    console.log(util.inspect(value, { maxArrayLength: Infinity }));
    
    0 讨论(0)
提交回复
热议问题