Styling Output from Command-line Node.js Scripts with Chalk

joshtronic

The command-line is powerful, but oftentimes it’s bland and boring. Dark text on a light background, light text on a dark background. It can be seriously bleak. Fortunately it doesn’t have to be a monochromatic wasteland. With the addition of chalk to your project, you can easily style and colorize script output, providing not just eye candy, but an improved user experience as well.

chalk is a library that provides a simple and easy to use interface for applying ANSI colors and styles to your command-line output. The package is exceptionally popular with, at the time of this writing, ~4.2 million weekly downloads and it is one of the most depended upon packages over on npm.

What’s even more impressive is that the library’s maintainer, Sindre Sorhus, is a full-time open source contributor and currently maintains over 1100 projects!

Getting Started

Before we can start cutting through the drab, we need to add chalk to our project:

# via yarn
$ yarn add chalk

# via npm
$ npm install chalk --save

And within our script, we will need to require it:

const chalk = require('chalk');

Usage

Using chalk is pretty painless, it works by chaining together colors and styles, finally passing in the string you’d like styled.

chalk is quite powerful, and in true UNIX philosophy, it does one thing well. Because of this, chalk by itself will only style your text, not output it. No big deal though, we can use the familiar console.log() to handle the output of the beautifully styled text.

Colorizing

With chalk we are able to colorize both the background and foreground of the output:

console.log(chalk.bgRed.green('Too early for Christmas?'));

Not ready for Christmas? That’s okay because we have a full palette of colors at our disposal:

  • black and bgBlack
  • red and bgRed
  • green and bgGreen
  • yellow and bgYellow
  • blue and bgBlue
  • magenta and bgMagenta
  • cyan and bgCyan
  • white and bgWhite
  • gray and bgBlackBright
  • redBright and bgRedBright
  • greenBright and bgGreenBright
  • yellowBright and bgYellowBright
  • blueBright and bgBlueBright
  • magentaBright and bgMagentaBright
  • cyanBright and bgCyanBright
  • whiteBright and bgWhiteBright

Styling

Styling works in the same way as colorizing output, just add it to the chain:

console.log(
  chalk.green.bgYellow.underline.bold('Alligators Rock!')
);

There’s more to it than just underlining and emphasizing text:

  • bold - make output bold
  • underline - make output underlined
  • dim - make output dim
  • inverse - swap foreground and background colors
  • reset - reset to the default color
  • hidden - hide output
  • visible - make output visible

And a couple of additional options that aren’t widely supported and should probably be avoided until support does improve:

  • italic - make output italicized
  • strikethrough - strike the output

Advanced Colorizing

You may have noticed that there were only 16 color pairs listed above, but most modern terminals are able to support 256 colors!

Instead of providing 256 different helper options, chalk provides support using the familiar hexadecimal color codes by way of the hex() and bgHex() options:

console.log(
  chalk.hex('#008f68').bgHex('#efbb35')('Alligators Rock!')
);

You’re not limited to just hexadecimal either, chalk supports a wide variety of different color models:

  • hex and bgHex - chalk.hex('#4aae9b')
  • rgb and bgRgb - chalk.rgb(74, 174, 155)
  • hsl and bgHsl - chalk.hsl(169, 40, 49)
  • hsv and bgHsv - chalk.hsv(47, 57, 68)
  • hwb and bgHwb - chalk.hwb(169, 29, 32)
  • keyword and bgKeyword - Accepts HTML/CSS color strings!

Nested Styles

All of these colors and styles are well and good, but what if we wanted to mix things up a bit? Perhaps we want to change the color and bold a single word in a string that’s already being styled by chalk.

Because the chalk methods can accept a comma separated list of arguments, similar to console.log() we can apply styles inside of styles:

console.log(
  chalk.orange(
    'I love', chalk.green.bold('Alligators'), ', don\'t you?'
  )
);

Bonus: Color Detection

Under the hood, chalk handles detecting whether or not colors are supported in the current terminal. As a convenience, this value is exposed, allowing you to create your own conditional logic around whether or not colors are supported:

if (!chalk.supportsColor) {
  throw new Error('Your terminal cannot handle this');
}
  Tweet It

🕵 Search Results

🔎 Searching...

Sponsored by #native_company# — Learn More
#native_title# #native_desc#
#native_cta#