How to print object in Node JS

Basic console.log will not go through long and complex object, and may decide to just print [Object] instead.

A good way to prevent that in node.js is to use util.inspect:

'use strict';
const util = require('util'),
    obj = /*Long and complex object*/;

console.log(util.inspect(obj, {depth: null}));
//depth: null tell util.inspect to open everything until it get to a circular reference, the result can be quite long however.

EDIT: In a pinch (in the REPL for example), a second option is JSON.stringify. No need to require it, but it will break on circular reference instead of printing the fact there is a reference.

Leave a Comment

deneme bonusudeneme bonusu veren sitelerOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casino