console.dir() is short for console.log(util.inspect()) (#tilPost)

Sometimes when debugging node scripts via console.log you’ll run into the situation, that console.log won’t show you the complete object you want to inspect.
console.log( { foo: { bar: { baz: { foo: ‘Show me!’ } } } } );
// { foo: {…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Sometimes when debugging node scripts via console.log you'll run into the situation, that console.log won't show you the complete object you want to inspect.

console.log( { foo: { bar: { baz: { foo: 'Show me!' } } } } );
// { foo: { bar: { baz: [Object] } } }

The solution to this problem is to use util.inspect which also includes the option to color the output.

console.log( util.inspect( { foo: { bar: { baz: { foo: 'Show me!' } } } }, { depth: null, colors: true } );
// { foo: { bar: { baz: { foo: 'Show me!' } } } }

Frederic Hemberger just told me that console.dir uses util.inspect under the hood, which means we can make it even shorter! ?

console.dir( { foo: { bar: { baz: { foo: 'Show me!' } } } }, { depth: null, colors: true } );
// { foo: { bar: { baz: { foo: 'Show me!' } } } }

Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate
APA
Stefan Judis | Sciencx (2022-12-08T05:27:53+00:00) » console.dir() is short for console.log(util.inspect()) (#tilPost). Retrieved from https://www.scien.cx/2017/03/12/console-dir-is-short-for-console-logutil-inspect-tilpost/.
MLA
" » console.dir() is short for console.log(util.inspect()) (#tilPost)." Stefan Judis | Sciencx - Sunday March 12, 2017, https://www.scien.cx/2017/03/12/console-dir-is-short-for-console-logutil-inspect-tilpost/
HARVARD
Stefan Judis | Sciencx Sunday March 12, 2017 » console.dir() is short for console.log(util.inspect()) (#tilPost)., viewed 2022-12-08T05:27:53+00:00,<https://www.scien.cx/2017/03/12/console-dir-is-short-for-console-logutil-inspect-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » console.dir() is short for console.log(util.inspect()) (#tilPost). [Internet]. [Accessed 2022-12-08T05:27:53+00:00]. Available from: https://www.scien.cx/2017/03/12/console-dir-is-short-for-console-logutil-inspect-tilpost/
CHICAGO
" » console.dir() is short for console.log(util.inspect()) (#tilPost)." Stefan Judis | Sciencx - Accessed 2022-12-08T05:27:53+00:00. https://www.scien.cx/2017/03/12/console-dir-is-short-for-console-logutil-inspect-tilpost/
IEEE
" » console.dir() is short for console.log(util.inspect()) (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2017/03/12/console-dir-is-short-for-console-logutil-inspect-tilpost/. [Accessed: 2022-12-08T05:27:53+00:00]
rf:citation
» console.dir() is short for console.log(util.inspect()) (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2017/03/12/console-dir-is-short-for-console-logutil-inspect-tilpost/ | 2022-12-08T05:27:53+00:00
https://github.com/addpipe/simple-recorderjs-demo