Adding colors to your Node.js console


The Colors module is one of the simpler modules you could use with Node. You can use it to provide different color and style effects to the console.log string output. All of the output that you generate in the Node.js console is of a single color. This can make debugging harder as you cannot easily differentiate the output of one log from another.

Take for example the last post about MySQL and Node.js. We logged all the post titles to the console with the following code. This printed the output using a single color.

for (var i in rows) {
    console.log('Post Titles: ', rows[i].post_title);
}

With the ‘Colors’ module we could easily add a color to the text with the following.

for (var i in rows) {
    console.log('Post Titles: ', rows[i].post_title.green);
}

Installing Colors

As usual you can install the module using npm.

npm install colors

Include this into your code.

var colors = require('colors');

Adding more variety

You can add more colors for different fields to quickly tell them apart.

for (var i in rows) {
    console.log(rows[i].post_title.green, rows[i].guid.yellow);
}

or make one field bold. Note that this will render differently on Windows and Linux.

for (var i in rows) {
    console.log(rows[i].post_title.green, rows[i].guid.bold);
}

The color modifiers can applied to strings and string objects. So if you do something like the following you will get an undefined error.

var number = 12;
console.log(number.red);

You will first need to convert the object to a string before applying the color operators.

var number = 12;
console.log(number.toString().red);

To print a JSON object, first serialize it and then apply the color operator. Taking the earlier example; suppose we want to print the complete row to the console rather than individual fields, we first need to serialize the JSON object.

for (var i in rows) {
    console.log(JSON.stringify(rows[i]).red);
}

To print each row in an alternating color you can do the following.

.
.
    var alt = true;
 
    for (var i in rows) {
        if(alt) {
            console.log(JSON.stringify(rows[i]).green);
            alt = false;
        } else {
            console.log(JSON.stringify(rows[i]).red);
            alt = true;
        }
    }

You can even chain operators for additional effects.

for (var i in rows) {
    console.log(JSON.stringify(rows[i]).bold.red); // Bold Red text
}

To make the code more modular we can also set a theme template.

colors.setTheme({
  normal: 'cyan',
  warn: 'yellow',
  debug: 'blue',
  error: 'red'
});
 
for (var i in rows) {
    console.log(rows[i].post_title.normal, rows[i].guid.debug);
}

A list of colors and styles supported is given below. Note that some of these will render differently on different operating systems.

bold, italic, underline, inverse, yellow, cyan, 
white, magenta, green, red, grey, blue, rainbow

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

1 Response

1

CodersBrothers

August 3rd, 2014 at 10:54 am

Your thoughts