Console Color References

From Logic Wiki
Jump to: navigation, search


Below you can find colors reference of text to command when running node.js application:

console.log('\x1b[36m%s\x1b[0m', 'I am cyan');  //cyan
console.log('\x1b[33m%s\x1b[0m', stringToMakeYellow);  //yellow

Note %s is where in the string (the second argument) gets injected. \x1b[0m resets the terminal color so it doesn't continue to be the chosen color anymore after this point.

Colors reference

Reset = "\x1b[0m"
Bright = "\x1b[1m"
Dim = "\x1b[2m"
Underscore = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"

FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"

BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"

There are a few modules for changing console font color in Node.js the most popular are:

  1. Chalk - https://github.com/chalk/chalk
  2. Colors - https://www.npmjs.org/package/colors
  3. Cli-color - https://www.npmjs.org/package/cli-color

chalk usage:

npm install chalk
var chalk = require('chalk');
console.log(chalk.red('Text in red'));

colors usage:

npm install colors
var colors = require('colors/safe'); // does not alter string prototype
console.log(colors.red('This String Will Display RED'));

There are a few colors to chose from as well as text formatting like Bold and Italic.

Many people have noted their disapproval at Colors altering the String prototype, if you prefer your prototypes left alone, you will want to use cli-color or chalk

cli-color usage:

npm install cli-color
var clc = require('cli-color');
console.log(clc.red('Text in red'));

Both cli-color and chalk require a bit more typing but you get similar results (to colors) without String prototype additions. Both support a good range of colors, formatting (bold/italics etc.) and have unit tests.


If you want to change the colors directly yourself without a module try

console.log('\x1b[36m', 'sometext' ,'\x1b[0m');

First '\x1b[36m' to change the colors to "36" and then back to terminal color "0".