🌜
🌞
chalk

chalk

v5.0.1

Terminal string styling done right

npm install chalk

README



Chalk


Terminal string styling done right

Coverage Status npm dependents Downloads run on repl.it Support Chalk on DEV





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)

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. The use of FORCE_COLOR overrides all other color support checks.

Explicit 256/Truecolor mode can be enabled using the --color=256 and --color=16m flags, respectively.

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.

Styles

Modifiers

  • reset - Reset the current style.
  • bold - Make the text bold.
  • dim - Make the text have lower opacity.
  • italic - Make the text italic. (Not widely supported)
  • underline - Put a horizontal line below the text. (Not widely supported)
  • overline - Put a horizontal line above the text. (Not widely supported)
  • inverse- Invert background and foreground colors.
  • hidden - Print the text but make it invisible.
  • strikethrough - Puts a horizontal line through the center of the text. (Not widely supported)
  • visible- Print the text only when Chalk has a color level above zero. Can be useful for things that are purely cosmetic.

Colors

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • blackBright (alias: gray, grey)
  • redBright
  • greenBright
  • yellowBright
  • blueBright
  • magentaBright
  • cyanBright
  • whiteBright

Background colors

  • bgBlack
  • bgRed
  • bgGreen
  • bgYellow
  • bgBlue
  • bgMagenta
  • bgCyan
  • bgWhite
  • bgBlackBright (alias: bgGray, bgGrey)
  • bgRedBright
  • bgGreenBright
  • bgYellowBright
  • bgBlueBright
  • bgMagentaBright
  • bgCyanBright
  • bgWhiteBright

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 31 (ANSI escape for red).

Examples:

  • chalk.hex('#DEADED').underline('Hello, world!')
  • chalk.rgb(15, 100, 204).inverse('Hello!')

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

  • chalk.bgHex('#DEADED').underline('Hello, world!')
  • chalk.bgRgb(15, 100, 204).inverse('Hello!')

The following color models can be used:

  • rgb - Example: chalk.rgb(255, 136, 0).bold('Orange!')
  • hex - Example: chalk.hex('#FF8800').bold('Orange!')
  • ansi256 - Example: chalk.bgAnsi256(194)('Honeydew, more or less')

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.

Origin story

colors.js used to be the most popular string styling module, but it has serious deficiencies like extending String.prototype which causes all kinds of problems and the package is unmaintained. Although there are other packages, they either do too much or not enough. Chalk is a clean and focused alternative.

chalk for enterprise

Available as part of the Tidelift Subscription.

The maintainers of chalk and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

Maintainers

Release Notes

5.0.1
By Sindre Sorhus • Published on March 8, 2022
  • Add main field to package.json for backwards compatibility with some developer tools 85f7e96

https://github.com/chalk/chalk/compare/v5.0.0...v5.0.1

5.0.0
By Sindre Sorhus • Published on November 26, 2021

Breaking

  • This package is now pure ESM. Please read this.
    • If you use TypeScript, you need to use TypeScript 4.7 or later. Why.
    • If you use a bundler, make sure it supports ESM and that you have correctly configured it for ESM.
    • The Chalk issue tracker is not a support channel for your favorite build/bundler tool.
    • It's totally fine to stay on Chalk v4. It's been stable for years.
  • Require Node.js 12.20 fa16f4e
  • Move some properties off the default export to individual named exports:
    • chalk.InstanceChalk
    • chalk.supportsColorsupportsColor
    • chalk.stderrchalkStderr
    • chalk.stderr.supportsColorsupportsColorStderr
  • Remove .keyword(), .hsl(), .hsv(), .hwb(), and .ansi() coloring methods (#433) 4cf2e40
    • These were not commonly used and added a lot of bloat to Chalk. You can achieve the same by using the color-convert package.
  • The tagged template literal support moved into a separate package: chalk-template (#524) c987c61 ```diff

-import chalk from 'chalk'; +import chalkTemplate from 'chalk-template';

-chalk2 + 3 = {bold ${2 + 3}}; +chalkTemplate2 + 3 = {bold ${2 + 3}};

```

Improvements

  • Bundle dependencies 04fdbd6
    • This means Chalk no longer has any dependencies 🎉
  • The install size is less than half of v4.
  • Add overline style (#433) 4cf2e40
  • Preserve function prototype methods (#434) 0fba91b

https://github.com/chalk/chalk/compare/v4.1.0...v5.0.0

4.1.2
By Sindre Sorhus • Published on July 30, 2021
  • Readme updates
4.1.1
By Sindre Sorhus • Published on April 21, 2021
4.1.0
By Sindre Sorhus • Published on June 9, 2020
  • Support template literals for nested calls (#392) 09ddbad

https://github.com/chalk/chalk/compare/v4.0.0...v4.1.0

4.0.0
By Sindre Sorhus • Published on April 2, 2020

Breaking

  • Require Node.js 10 61999a4
  • Change the Level TypeScript type to be a union instead of enum f0f4638 ```diff

-if (chalk.level > Level.None) {} +if (chalk.level > 0) {}

```

Improvements

  • Use Object.setPrototypeOf as __proto__ could potentially be disabled (#387) 63469d3

https://github.com/chalk/chalk/compare/v3.0.0...v4.0.0

3.0.0
By Sindre Sorhus • Published on November 9, 2019

This release has been in development for more than a year and massively improves performance and the time it takes to import Chalk.

Thanks to @stroncium for his hard work on this. 🙌

Breaking

  • Require Node.js 8 3ef170b
  • Remove the .enabled property in favor of .level (#356) 1f77953
    • Why: It was confusing to users to have two properties for detecting color support.
    • Migrate: ```diff

-if (chalk.enabled) {} +if (chalk.level > 0) {}

- Remove `chalk.constructor()` in favor of `chalk.Instance()` (#322)  de2f4cd
    - Migrate:
```diff
-new chalk.constructor({level: 1});
+new chalk.Instance({level: 1})

Minor breaking

  • Use CommonJS-compatible export in TypeScript definition (#344) 98628d9

-import chalk from 'chalk'; +import chalk = require('chalk');

- Drop built-in Flow type definition  d3be9c6
    - Why: None of us use Flow and we were not good at maintaining the type definition. You can get the types at [flow-typed](https://github.com/flow-typed/flow-typed) *(needs to be updated to Chalk v3, open an issue on `flow-typed`)*.
- Rename the `ChalkOptions` TypeScript type to `Options` cf66156
- Remove `dim` style workaround for Windows (#331)  cd5de7a
    - Why: The issue was fixed in newer Windows 10 builds.
- Remove the `blue` color workaround for Windows (#330)  2ca015c
    - Why: The issue was fixed in newer Windows 10 builds.

### Enhancements

- Massively improve performance! (#337)  c08417e
- Improve require speed (#358)  61aca7c
- Add [`chalk.stderr`](https://github.com/chalk/chalk#chalkstderr-and-chalkstderrsupportscolor) for printing to stderr (#359)  2a53389
- Add `blackBright` color. It's the same as the `gray` color, but added for consistency. c25c32a
- Fix support for bracketed Unicode escapes in template literals (#350)  9830948
- Export TypeScript types for colors and modifiers (#357)  6b4d206
- Add `ansi256` and `bgAnsi256` to TypeScript declaration (#368)  fb8e85a
- Add `ansi` and `bgAnsi` to TypeScript declaration (#369)  18c280d

### Color detection

- The `FORCE_COLOR` environment variable can now be used to force a certain color level (#315)  af4a078
- Add support for GitHub Actions in the color detection https://github.com/chalk/supports-color/commit/79d13032e2aa7a011f1c8badc866bcf4bc500f7a
- Give `TERM=dumb` higher priority in the color detection https://github.com/chalk/supports-color/commit/8d6a7b5830a96858a95ce9cfee1840dc30a3e837
- Add support for VT220 in the color detection https://github.com/chalk/supports-color/commit/ed0fe39d600ff1c286b3948abbef88eaef4f8f27

### Fixes

- Fix support for nested styles (#335)  87156ce
- Fix const enum for TypeScript (#364)  4e65299
- Fix TypeScript type for `supportsColor` which is top‑level only (#342)  b3e9b91
- Fix TypeScript type for `chalk.supportsColor` (#347)  d82b2a6
- Fix TypeScript type for tagged template literal argument to accept `unknown` instead of just `string` (#316)  7f6e563

https://github.com/chalk/chalk/compare/v2.4.1...v3.0.0
3.0.0-beta.2
By Sindre Sorhus • Published on October 8, 2019

While this is a beta release, the code is well-tested and fairly stable.
We encourage you to test it out and report any issues.

$ npm install [email protected]

See the beta 1 release notes for more.


  • Add ansi256 and bgAnsi256 to TypeScript declarations (#368) fb8e85a

https://github.com/chalk/chalk/compare/v3.0.0-beta.1...v3.0.0-beta.2

3.0.0-beta.1
By Sindre Sorhus • Published on September 27, 2019

This release has been in development for more than a year and massively improves performance and the time it takes to import Chalk. Thanks to @stroncium for his hard work on this.


While this is a beta release, the code is well-tested and fairly stable.
We encourage you to test it out and report any issues.

$ npm install [email protected]

Breaking

  • Require Node.js 8 3ef170b
  • Remove the .enabled property in favor of .level (#356) 1f77953
    • Why: It was confusing to users to have two properties for this.
    • Migrate: ```diff

-if (chalk.enabled) {} +if (chalk.level > 0) {}

- Remove `chalk.constructor()` in favor of `chalk.Instance()` (#322)  de2f4cd
    - Migrate:
```diff
-new chalk.constructor({level: 1});
+new chalk.Instance({level: 1})

Minor breaking

  • Use CommonJS-compatible export in TypeScript definition (#344) 98628d9

-import chalk from 'chalk'; +import chalk = require('chalk');

- Drop built-in Flow type definition  d3be9c6
    - Why: None of us use Flow and we were not good at maintaining the type definition. You can get the types at [flow-typed](https://github.com/flow-typed/flow-typed) *(needs to be updated to Chalk v3)*.
- Rename the `ChalkOptions` TypeScript type to `Options` cf66156
- Remove `dim` style workaround for Windows (#331)  cd5de7a
    - Why: The issue was fixed in newer Windows 10 builds.
- Remove the `blue` color workaround for Windows (#330)  2ca015c
    - Why: The issue was fixed in newer Windows 10 builds.

### Enhancements

- Massively improve performance (#337)  c08417e
- Improve require speed (#358)  61aca7c
- Add [`chalk.stderr`](https://github.com/chalk/chalk#chalkstderr-and-chalkstderrsupportscolor) for printing to stderr (#359)  2a53389
- Add `blackBright` color. It's the same as the `gray` color, but added for consistency. c25c32a
- Fix support for bracketed Unicode escapes in template literals (#350)  9830948
- Export TypeScript types for colors and modifiers (#357)  6b4d206

### Color detection

- The `FORCE_COLOR` environment variable can now be used to force a certain color level (#315)  af4a078
- Add support for GitHub Actions in the color detection https://github.com/chalk/supports-color/commit/79d13032e2aa7a011f1c8badc866bcf4bc500f7a
- Give `TERM=dumb` higher priority in the color detection https://github.com/chalk/supports-color/commit/8d6a7b5830a96858a95ce9cfee1840dc30a3e837
- Add support for VT220 in the color detection https://github.com/chalk/supports-color/commit/ed0fe39d600ff1c286b3948abbef88eaef4f8f27

### Fixes

- Fix support for nested styles (#335)  87156ce
- Fix const enum for TypeScript (#364)  4e65299
- Fix TypeScript type for `supportsColor` which is top‑level only (#342)  b3e9b91
- Fix TypeScript type for `chalk.supportsColor` (#347)  d82b2a6
- Fix TypeScript type for tagged template literal argument to accept `unknown` instead of just `string` (#316)  7f6e563

https://github.com/chalk/chalk/compare/v2.4.1...v3.0.0-beta.1
2.4.2
By Sindre Sorhus • Published on January 5, 2019

This release is done from the v2-release branch, as master branch targets the work-in-progress v3 release.

General

License
MIT
Typescript Types
Built-in
Tree-shakeable
Yes

Popularity

GitHub Stargazers
19.2K
Community Interest
19.2K
Number of Forks
784

Maintenance

Commits
10/219/2206
Last Commit
Open Issues
0
Closed Issues
332
Open Pull Requests
0
Closed Pull Requests
67

Versions

Versions Released
10/219/2201
Latest Version Released
Mar 8, 2022
Current Tags
latest5.0.1
next3.0.0-beta.2

Contributors

sindresorhus
sindresorhus
Commits: 159
Qix-
Qix-
Commits: 34
Richienb
Richienb
Commits: 11
stroncium
stroncium
Commits: 3
stevemao
stevemao
Commits: 3
kevva
kevva
Commits: 3
pedrottimark
pedrottimark
Commits: 3
LitoMore
LitoMore
Commits: 2
toonijn
toonijn
Commits: 2
calebboyd
calebboyd
Commits: 2
seanmonstar
seanmonstar
Commits: 2
popey456963
popey456963
Commits: 2
nazrhyn
nazrhyn
Commits: 2
bokub
bokub
Commits: 2
SimenB
SimenB
Commits: 1