NodeJS Utilities: About I/O and Debugging

Home / NodeJS Utilities: About I/O and Debugging

NodeJS Utilities: About I/O and Debugging

December 9, 2015 | Article | No Comments

Utilities, like implied by the name, is utilities employed for some distinct purpose. These utilities are provided by NodeJS through global objects.

console

Node provides a global “console” object to which we can output strings. However, the output is classified into some mode, according to the output stream it print the output to.

.log()

If you want to print data to stdout, it’s as simple as writing following code:

console.log("Hello World!");

Which will print “Hello World!”. The data (string in that case) is streamed out after formatting it. It is mainly used for simple output and instead of string we can also output an object, like this:

var a = {1: true, 2: false};
console.log(a);    // => {'1': true, '2': false}

We can also use string interpolation to print out things, like:

var a = {1: true, 2: false};
console.log('This is a number: %d, and this is a stirng: %s, ' +
            'and this is an object outputted as JSON: %j',
            42, 'Hello', a);

Which in turns print:

This is a number: 42, and this is a stirng: Hello, and this is an object outputted as JSON: {"1": true, "2": false}

If you are familiar with C or C++, you might find it similar with C’s printf() function. The placeholder is similar, you use %d for number (integer and floating point number), %s for string, and %j for JSON which is not exists in C’s formatting.

.warn()

If you want to print out to stderr, you can do this:

console.warn("Warning!!");

.trace()

And to print stack trace, you can do:

console.trace();

For stack trace, you will be presented by the current stack condition.

util

util is a module, which bundles some functions. To use this module, we need to include the util module:

var util = require('util');

.log()

var util = require('util');
util.log('Hello');

Similar to console.log(), however it’s slightly different. The util.log() will print current timestamp and given string in which build a line like this: Mar 17:11:09 – Hello

.inspect()

There is also a handy function, inspect(), which is nice for quick debugging by inspecting and printing an object properties.

var util = require('util');
var a = {1: true, 2: false};
console.log(util.inspect(a));

We can give more arguments to util.inspect() which are in following format:

util.inspect(object, showHidden, depth = 2, showColors);

showHidden is argument which inspect non-enumerable properties if it is turned on. This properties are belong to the object prototype chain, not the object itself. The depth, third argument, is the default depth on the object graph it should show. This is useful when inspecting large objects. To recurse indefinitely, pass a null value.

An important note: util.inspect keeps track of the visited objects. If there is a circular dependencies, a string “[Circular]” will appear on the outputted string.

,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial