chalk README

The project's README from github.com/chalk/chalk. As one markdown file: README.md.



Chalk


Terminal string styling done right

Coverage Status npm dependents Downloads

Info

Highlights

Install

npm install chalk

IMPORTANT: Chalk 5 is ESM. If you want to use Chalk with TypeScript or a build tool, you will probably want to use Chalk 4 for now. Read more.

Usage

import chalk from 'chalk';

console.log(chalk.blue('Hello world!'));

Chalk comes with an easy to use composable API where you just chain and nest the styles you want.

import chalk from 'chalk';

const log = console.log;

// Combine styled and normal strings
log(chalk.blue('Hello') + ' World' + chalk.red('!'));

// Compose multiple styles using the chainable API
log(chalk.blue.bgRed.bold('Hello world!'));

// Pass in multiple arguments
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));

// Nest styles
log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));

// Nest styles of the same type even (color, underline, background)
log(chalk.green(
	'I am a green line ' +
	chalk.blue.underline.bold('with a blue substring') +
	' that becomes green again!'
));

// ES2015 template literal
log(`
CPU: ${chalk.red('90%')}
RAM: ${chalk.green('40%')}
DISK: ${chalk.yellow('70%')}
`);

// Use RGB colors in terminal emulators that support it.
log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
log(chalk.hex('#DEADED').bold('Bold gray!'));

Easily define your own themes:

import chalk from 'chalk';

const error = chalk.bold.red;
const warning = chalk.hex('#FFA500'); // Orange color

console.log(error('Error!'));
console.log(warning('Warning!'));

Take advantage of console.log string substitution:

import chalk from 'chalk';

const name = 'Sindre';
console.log(chalk.green('Hello %s'), name);
//=> 'Hello Sindre'

API

chalk.<style>[.<style>...](string, [string...])

Example: chalk.red.bold.underline('Hello', 'world');

Chain styles and call the last one as a method with a string argument. Order doesn't matter, and later styles take precedent in case of a conflict. This simply means that chalk.red.yellow.green is equivalent to chalk.green.

Multiple arguments will be separated by space.

chalk.level

Specifies the level of color support.

Color support is automatically detected, but you can override it by setting the level property. You should however only do this in your own code as it applies globally to all Chalk consumers.

If you need to change this in a reusable module, create a new instance:

import {Chalk} from 'chalk';

const customChalk = new Chalk({level: 0});
Level Description
0 All colors disabled
1 Basic color support (16 colors)
2 256 color support
3 Truecolor support (16 million colors)

Both the level option and the level property throw for anything that is not an integer from 0 to 3. Omit the option, or pass undefined, to have the level detected instead.

supportsColor

Detect whether the terminal supports color. Used internally and handled for you, but exposed for convenience.

Can be overridden by the user with the flags --color and --no-color. For situations where using --color is not possible, use the environment variable FORCE_COLOR=1 (level 1), FORCE_COLOR=2 (level 2), or FORCE_COLOR=3 (level 3) to forcefully enable color, or FORCE_COLOR=0 to forcefully disable. A numeric FORCE_COLOR overrides the detected color support and sets the level directly, meaning the terminal cannot raise it to a higher level. Use FORCE_COLOR=true to instead only enable color and let the level be detected.

Explicit 256/Truecolor mode can be enabled using the --color=256 and --color=16m flags, respectively. These take precedence over a non-zero numeric FORCE_COLOR.

chalkStderr and supportsColorStderr

chalkStderr contains a separate instance configured with color support detected for stderr stream instead of stdout. Override rules from supportsColor apply to this too. supportsColorStderr is exposed for convenience.

modifierNames, foregroundColorNames, backgroundColorNames, underlineColorNames, and colorNames

All supported style strings are exposed as an array of strings for convenience. colorNames is the combination of foregroundColorNames and backgroundColorNames. Underline color names are kept separate in underlineColorNames.

This can be useful if you wrap Chalk and need to validate input:

import {modifierNames, foregroundColorNames} from 'chalk';

console.log(modifierNames.includes('bold'));
//=> true

console.log(foregroundColorNames.includes('pink'));
//=> false

Styles

Modifiers

Colors

Background colors

Underline colors

The underline color is set independently of the text color, so the color is only visible when an underline style is also applied. For example, chalk.underlineRed.underlineCurly('typo') renders a red squiggle below otherwise unstyled text. (Not widely supported)

Unlike text and background colors, there is no basic 16-color form for underline colors, so they always use the 256-color escape. At level 1 they are downsampled to the first 16 palette entries rather than to a basic color code.

256 and Truecolor color support

Chalk supports 256 colors and Truecolor (16 million colors) on supported terminal apps.

Colors are downsampled from 16 million RGB values to an ANSI color format that is supported by the terminal emulator (or by specifying {level: n} as a Chalk option). For example, Chalk configured to run at level 1 (basic color support) will downsample an RGB value of #FF0000 (red) to 91 (ANSI escape for bright red). The same applies to ansi256 values, so chalk.ansi256(196) also becomes 91 at level 1.

Examples:

Background versions of these models are prefixed with bg and the first letter of the model capitalized (e.g. hex for foreground colors and bgHex for background colors).

Underline versions are prefixed with underline in the same way (e.g. hex for foreground colors and underlineHex for underline colors). They only take effect when an underline style is also applied.

The following color models can be used:

Browser support

Since Chrome 69, ANSI escape codes are natively supported in the developer console.

Windows

If you're on Windows, do yourself a favor and use Windows Terminal instead of cmd.exe.

FAQ

Why not switch to a smaller coloring package?

Chalk may be larger, but there is a reason for that. It offers a more user-friendly API, well-documented types, supports millions of colors, and covers edge cases that smaller alternatives miss. Chalk is mature, reliable, and built to last.

But beyond the technical aspects, there's something more critical: trust and long-term maintenance. I have been active in open source for over a decade, and I'm committed to keeping Chalk maintained. Smaller packages might seem appealing now, but there's no guarantee they will be around for the long term, or that they won't become malicious over time.

Chalk is also likely already in your dependency tree (since 100K+ packages depend on it), so switching won’t save space—in fact, it might increase it. npm deduplicates dependencies, so multiple Chalk instances turn into one, but adding another package alongside it will increase your overall size.

If the goal is to clean up the ecosystem, switching away from Chalk won’t even make a dent. The real problem lies with packages that have very deep dependency trees (for example, those including a lot of polyfills). Chalk has no dependencies. It's better to focus on impactful changes rather than minor optimizations.

If absolute package size is important to you, I also maintain yoctocolors, one of the smallest color packages out there.

- Sindre

But the smaller coloring package has benchmarks showing it is faster

Micro-benchmarks are flawed because they measure performance in unrealistic, isolated scenarios, often giving a distorted view of real-world performance. Don't believe marketing fluff. All the coloring packages are more than fast enough.

(Not accepting additional entries)

Maintainers

chalk (chalk/chalk)

Searching Database…

23.3K ·GitHub
这是干什么用的?Chalk 是一个用来给终端输出设置样式的 JavaScript 库:它把你的字符串包上 ANSI 转义码,让控制台文字可以变色、加粗、加下划线等等。大约有 115,000 个软件包依赖它。
它是需要 import 的库,还是命令行工具?是一个用于 Node.js 代码的库:你可以像 chalk.red.bold('Hi') 这样链式组合样式,然后把结果打印出来。另有一个独立的包 chalk-cli 提供命令行样式。
怎么把它装到我的项目里?先 npm install chalk,然后在需要的地方 import 它。有一点要注意:Chalk 5 只支持 ESM,所以如果你用 TypeScript 或基于打包工具的构建,README 建议继续用 Chalk 4。
它会不会给我项目拖进来一大堆依赖?不会——Chalk 零依赖,除了它自己什么都不额外添加。当前的大版本面向新版 Node.js(仓库里的 engines 要求 Node 22 以上)。
如果我的终端对颜色的支持很差怎么办?Chalk 会自动检测支持程度并降级:level 0 关闭颜色,1 是 16 色,2 是 256 色,3 是真彩色。用户可以通过 chalk.level、--color/--no-color 标志或 FORCE_COLOR 环境变量来强制开启或关闭。
在 Windows 上能用吗?能——样式在任何终端里都能用,不过 README 特别推荐用 Windows Terminal,而不是老旧的 cmd.exe。
开箱即用提供了哪些样式和颜色?有 bold、dim、italic、underline(还有 curly、dotted、dashed 变体)、strikethrough、inverse、hidden 等修饰符,外加 16 色及其亮色变体、对应的背景色,以及可以单独设置颜色的下划线。
可以用 hex 或 RGB 这样的精确颜色吗?可以——chalk.hex('#FF8800')、chalk.rgb(15, 100, 204) 和 chalk.ansi256(194) 能输出 256 色和真彩色,加 bg 前缀的版本用于背景色。如果终端支持的颜色更少,数值会自动降采样。
比起 yoctocolors 这种更小的包,为什么选 Chalk?按维护者的说法:API 更友好、类型文档齐全、支持上百万种颜色、对各种边界情况处理得更好,而且他已经承诺长期维护十多年了。反正大多数项目的依赖树里本来就有 Chalk;如果你真的特别在意体积,同一位作者还维护着 yoctocolors。
What are similar projects?xterm.js, lipgloss, wezterm, upterm, ttfx, blessed-contrib, nub, ink
Creator
chalk
Size
1 MB
Stars
23.3K
Age
13y
Commits · 90 Days