Guide · Open source
How to use chalk: colored terminal output in Node.js
Updated · written with AI from the project's own documentation and checked against our catalogue data
Chalk is a JavaScript library for styling text printed to a terminal, such as making words red, bold, or underlined. It is aimed at Node.js developers who build command-line tools or want color-coded logs and status messages. After this guide you will be able to install Chalk, chain styles, use RGB and hex colors, and control when color is shown.
Quick start
- 1Install Chalk. Run npm install chalk in your Node.js project. Choose Chalk 4 if you use TypeScript or a build tool that cannot handle ESM.
- 2Import Chalk. Add import chalk from 'chalk'; at the top of your script.
- 3Style your text. Chain style names such as chalk.blue.bold and call them with a string.
- 4Nest or combine styles. Nest styled substrings inside other styled strings, or join styled and plain text with +.
- 5Set color support if needed. Use FORCE_COLOR or --color and --no-color flags to force or disable color, or create a new Chalk instance with a level.
- 6Print to the terminal. Use console.log to output the styled text and check it in a terminal that supports color.
Install Chalk with npm
Chalk is installed through the npm package manager and needs Node.js. Run the install command in your project folder.
Chalk 5 is an ES module. If you use TypeScript or a build tool, the README suggests you will probably want Chalk 4 for now.
npm install chalkPrint your first colored line
Import Chalk and wrap a string in a style. The README example uses ESM syntax and prints blue text with console.log.
import chalk from 'chalk';
console.log(chalk.blue('Hello world!'));Chain and nest styles
Chain style names with dots and call the last one with a string. Order does not matter, and later styles win when two conflict, so chalk.red.yellow.green is equivalent to chalk.green. Multiple arguments are joined with a space. You can also combine plain strings with styled ones and nest styles inside each other.
Styles you can chain include modifiers such as bold, dim and underline, colors such as red and blue, and background colors such as bgRed and bgBlue. Some modifiers, including italic, underline and strikethrough, are marked in the README as not widely supported.
- Combine styled and plain text with +, for example chalk.blue('Hello') + ' World'.
- Pass several arguments, which are separated by spaces.
- Nest styles, for example chalk.red('Hello', chalk.underline.bgBlue('world') + '!').
- Use the %s substitution form with console.log if you prefer it.
import chalk from 'chalk';
console.log(chalk.blue.bgRed.bold('Hello world!'));
console.log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));Use RGB, hex and 256 colors
Chalk supports 256 colors and Truecolor (16 million colors) in terminal apps that support them. Colors are downsampled to a format the terminal can show. For example, at level 1 an RGB value of #FF0000 becomes 91, the ANSI escape for bright red.
The underline colors such as underlineRed are set separately from the text color and only show when an underline style is also applied.
import chalk from 'chalk';
console.log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
console.log(chalk.hex('#DEADED').bold('Bold gray!'));
console.log(chalk.underlineRed.underlineCurly('typo'));Control color support
Chalk detects whether the terminal supports color and downgrades colors when it cannot show them. The level property shows the support level: 0 disables colors, 1 gives basic 16 colors, 2 gives 256 colors, and 3 gives Truecolor.
You can override detection with the --color and --no-color flags, or with the FORCE_COLOR environment variable. Because overriding the level applies globally, the README advises doing it only in your own code. In a reusable module, create a new instance instead.
- FORCE_COLOR=1, 2 or 3 forces color at that level.
- FORCE_COLOR=0 turns color off.
- FORCE_COLOR=true enables color and lets the level be detected.
- --color=256 and --color=16m request 256 or Truecolor mode explicitly.
import {Chalk} from 'chalk';
const customChalk = new Chalk({level: 0});Define your own themes and use stderr
You can store a style chain in a variable and reuse it as a theme. Chalk also exposes chalkStderr, a separate instance configured for the stderr stream instead of stdout, which is useful when your errors go to stderr.
The package also exports lists of style names, such as modifierNames and foregroundColorNames, which help you validate user input.
import chalk from 'chalk';
const error = chalk.bold.red;
const warning = chalk.hex('#FFA500');
console.log(error('Error!'));
console.log(warning('Warning!'));What people use it for
- Coloring CPU, RAM and disk percentages in a status printout.
- Showing error, warning and info messages in different colors in a command-line tool.
- Highlighting part of a line, such as a blue substring inside green text, in log output.
- Using hex or RGB colors in terminals that support Truecolor.
Frequently asked questions
How do I install chalk?
Install it from npm with npm install chalk. The README lists Node.js as the prerequisite. Note that Chalk 5 is ESM, so if you use TypeScript or a build tool, the README suggests Chalk 4.
Is chalk free to use?
Chalk is released under the MIT licence, as listed in the project details. The source does not describe pricing beyond that.
Does chalk work with TypeScript?
The README says Chalk 5 is ESM and, if you use TypeScript or a build tool, you will probably want Chalk 4 for now. Check the Chalk 5 release notes linked in the README before upgrading.
How do I turn chalk colors off?
Use the --no-color flag, or set FORCE_COLOR=0 to forcefully disable color. You can also create a Chalk instance with level 0.
What is a smaller alternative to chalk?
The README points to yoctocolors as a smaller alternative. Other comparable projects listed are picocolors, kleur and ansi-colors.
Can chalk be used in a web browser?
No. Chalk is for styling text printed to the terminal, so it is not suited to colored output in a web browser.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
Chalk suits Node.js developers building command-line tools or colored logs who want an expressive API and automatic color detection. If you use TypeScript or build tools that cannot handle ESM, use Chalk 4, and if you need the smallest possible footprint, the README points to yoctocolors instead.
Install options & downloads