When using Node.JS and you need to write to your log files what the contents of an OBJECT is, simply do the following:
- Require the Utility module at the setup of your script. Put this at the top where you are requiring all of your other modules.1var util = require('util');
- Then, where you want to view the contents of your object, simply use console.log with the utility.inspect function. Here is an example:1<span class="pln">console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">( </span><span class="pln">util</span><span class="pun">.</span><span class="pln">inspect</span><span class="pun">( objectName </span><span class="pln">) );</span>
Simple as that!
The Utility module offers many other features, but that’s to be explored another day.