colors.js

Build Status version dependencies devDependencies

Please check out the roadmap for upcoming features and releases. Please open Issues to provide feedback, and check the develop branch for the latest bleeding-edge updates.

get color and style in your node.js console

Demo

Installation

  1. npm install colors

colors and styles!

text colors

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • gray
  • grey

background colors

  • bgBlack
  • bgRed
  • bgGreen
  • bgYellow
  • bgBlue
  • bgMagenta
  • bgCyan
  • bgWhite

styles

  • reset
  • bold
  • dim
  • italic
  • underline
  • inverse
  • hidden
  • strikethrough

extras

  • rainbow
  • zebra
  • america
  • trap
  • random

Usage

By popular demand, colors now ships with two types of usages!

The super nifty way

  1. var colors = require('colors');
  2. console.log('hello'.green); // outputs green text
  3. console.log('i like cake and pies'.underline.red) // outputs red underlined text
  4. console.log('inverse the color'.inverse); // inverses the color
  5. console.log('OMG Rainbows!'.rainbow); // rainbow
  6. console.log('Run the trap'.trap); // Drops the bass

or a slightly less nifty way which doesn’t extend String.prototype

  1. var colors = require('colors/safe');
  2. console.log(colors.green('hello')); // outputs green text
  3. console.log(colors.red.underline('i like cake and pies')) // outputs red underlined text
  4. console.log(colors.inverse('inverse the color')); // inverses the color
  5. console.log(colors.rainbow('OMG Rainbows!')); // rainbow
  6. console.log(colors.trap('Run the trap')); // Drops the bass

I prefer the first way. Some people seem to be afraid of extending String.prototype and prefer the second way.

If you are writing good code you will never have an issue with the first approach. If you really don’t want to touch String.prototype, the second usage will not touch String native object.

Disabling Colors

To disable colors you can pass the following arguments in the command line to your application:

  1. node myapp.js --no-color

Console.log string substitution

  1. var name = 'Marak';
  2. console.log(colors.green('Hello %s'), name);
  3. // outputs -> 'Hello Marak'

Custom themes

Using standard API

  1. var colors = require('colors');
  2. colors.setTheme({
  3. silly: 'rainbow',
  4. input: 'grey',
  5. verbose: 'cyan',
  6. prompt: 'grey',
  7. info: 'green',
  8. data: 'grey',
  9. help: 'cyan',
  10. warn: 'yellow',
  11. debug: 'blue',
  12. error: 'red'
  13. });
  14. // outputs red text
  15. console.log("this is an error".error);
  16. // outputs yellow text
  17. console.log("this is a warning".warn);

Using string safe API

  1. var colors = require('colors/safe');
  2. // set single property
  3. var error = colors.red;
  4. error('this is red');
  5. // set theme
  6. colors.setTheme({
  7. silly: 'rainbow',
  8. input: 'grey',
  9. verbose: 'cyan',
  10. prompt: 'grey',
  11. info: 'green',
  12. data: 'grey',
  13. help: 'cyan',
  14. warn: 'yellow',
  15. debug: 'blue',
  16. error: 'red'
  17. });
  18. // outputs red text
  19. console.log(colors.error("this is an error"));
  20. // outputs yellow text
  21. console.log(colors.warn("this is a warning"));

Combining Colors

  1. var colors = require('colors');
  2. colors.setTheme({
  3. custom: ['red', 'underline']
  4. });
  5. console.log('test'.custom);

Protip: There is a secret undocumented style in colors. If you find the style you can summon him.