Bring colors and life to your console outputs in Node

2016-08-09

This article is a showcase of a popular node library to display colors in the console.

The gist

Here is the code you need to get started:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var colors = require('colors');

// Text color
console.log(colors.red('Love, exciting and new'));

// Invert colors (including background)
console.log(colors.inverse('Come aboard, we\'re expecting you'));

// Custom style
console.log(colors.rainbow('Love, life\'s sweetest reward'));

// Background Color
console.log(colors.bgMagenta('Let it flow, it floats back to you'));

// Combine different properties
console.log(colors.bgGreen.black('The Love Boat soon will be making another run'));


Run it and you will see the following in your console:


Don’t forget to npm install the library with:

1
$ npm install --save colors

String prototype

The library will also add string prototypes to simplify the code, use whichever you prefer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Text color
console.log(('Love, exciting and new').red);

// Invert colors (including background)
console.log(('Come aboard, we\'re expecting you').inverse);

// Custom style
console.log(('Love, life\'s sweetest reward').rainbow);

// Background Color
console.log(('Let it flow, it floats back to you').bgMagenta);

// Combine different properties
console.log(('The Love Boat soon will be making another run').bgGreen.black);

Easter Egg

The library mentions a “a secret undocumented style”. Here it is for those too lazy to look it up.