What is chalk?
The chalk npm package is a popular library for styling and coloring text in the terminal. It provides an easy-to-use API for applying various text styles, such as color, background color, bold, underline, and more, to console output in Node.js applications.
What are chalk's main functionalities?
Text color
Change the color of the text. In this example, the text 'Hello world!' will be printed in blue.
console.log(chalk.blue('Hello world!'));
Background color
Change the background color of the text. Here, 'Hello world!' will have a red background.
console.log(chalk.bgRed('Hello world!'));
Text styles
Apply text styles such as bold, italic, underline, etc. This code sample makes the text 'Hello world!' bold.
console.log(chalk.bold('Hello world!'));
Combining styles
Combine multiple styles together. The text 'Hello world!' will be bold with blue text and a red background.
console.log(chalk.blue.bgRed.bold('Hello world!'));
Composing multiple styles
Create reusable composed style functions. This creates an 'error' style that is bold and red, which can be used to print error messages.
const error = chalk.bold.red; console.log(error('Error!'));
Template literals
Use tagged template literals for styling. This allows for more readable code when applying multiple styles.
console.log(chalk`{blue.bold Hello} {red world!}`);
Other packages similar to chalk
colors
The 'colors' package is similar to chalk and allows for coloring and styling terminal output. Unlike chalk, 'colors' extends String.prototype to add color and style methods directly to strings, which some may find less clean than chalk's functional approach.
cli-color
The 'cli-color' package provides similar functionality to chalk with a focus on performance. It offers a chainable API and additional features like column alignment and line width control, but it might be more complex to use than chalk.
ansi-styles
The 'ansi-styles' package is a lower-level library that chalk itself uses. It provides ANSI escape codes for styling text in the terminal. It's more manual and less user-friendly than chalk but offers more control for those who need it.
ink
While 'ink' is not a direct alternative to chalk, it is a React-based rendering library for interactive command-line apps that includes its own system for styling text. It's more powerful for building full CLI applications but is overkill for simple text styling.
chalk
Terminal string styling done right.
colors.js is currently the most popular string styling module, but it has serious deficiencies like extending String.prototype which causes all kinds of problems. Although there are other ones, they either do too much or not enough.
Chalk is a clean and focused alternative.
Why
- Doesn't extend String.prototype
- Expressive API
- Clean and focused
- Auto-detects color support
- Actively maintained
Install
Install with npm: npm install --save chalk
Example
Chalk comes with an easy to use composable API where you just chain the styles you want.
var chalk = require('chalk');
console.log(chalk.blue('Hello world!'));
console.log(chalk.blue('Hello'), 'World' + chalk.red('!'));
console.log(chalk.blue.bgRed.bold('Hello world!'));
chalk.red('Hello', chalk.underline.bgBlue('world') + '!');
console.log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'))
You can easily define your own themes.
var chalk = require('chalk');
var error = chalk.bold.red;
console.log(error('Error!'));
API
chalk.<style>[.<style>...](string)
Chain styles and call the last one as a method with a string argument.
Multiple arguments are also supported. Chalk will add a space between each one.
chalk.enabled
Color support is automatically detected, but you can override it.
chalk.supportsColor
Detect whether the terminal supports color.
Can be overridden by the user with the flags --color
and --no-color
.
Used internally and handled for you, but exposed for convenience.
chalk.styles
Exposes the styles as ANSI escape codes.
var chalk = require('chalk');
console.log(chalk.styles.red);
console.log(chalk.styles.red[0] + 'Hello' + chalk.styles.red[1]);
chalk.stripColor(string)
Strip color from a string.
Styles
General
- reset
- bold
- italic
- underline
- inverse
- strikethrough
Text colors
- black
- red
- green
- yellow
- blue
- magenta
- cyan
- white
- gray
Background colors
- bgBlack
- bgRed
- bgGreen
- bgYellow
- bgBlue
- bgMagenta
- bgCyan
- bgWhite
License
MIT License • © Sindre Sorhus