Chalk

Terminal string styling done right

README



Chalk



Terminal string styling done right

Coverage Status npm dependents Downloads run on repl.it

undefined




Sindre Sorhus' open source work is supported by the community on GitHub Sponsors

Special thanks to:





Strapi
Strapi is the leading open-source headless CMS.
It’s 100% JavaScript, fully customizable, and developer-first.


StackAid
Fund your open source dependencies






Highlights


- Expressive API
- Highly performant
- No dependencies
- Ability to nest styles
- Auto-detects color support
- Doesn't extend String.prototype
- Clean and focused
- Actively maintained
- Used by ~86,000 packages as of October 4, 2022

Install


  1. ```sh
  2. npm install chalk
  3. ```

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


  1. ```js
  2. import chalk from 'chalk';

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

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

  1. ```js
  2. import chalk from 'chalk';

  3. const log = console.log;

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

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

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

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

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

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

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

Easily define your own themes:

  1. ```js
  2. import chalk from 'chalk';

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

  5. console.log(error('Error!'));
  6. console.log(warning('Warning!'));
  7. ```

Take advantage of console.log string substitution:

  1. ```js
  2. import chalk from 'chalk';

  3. const name = 'Sindre';
  4. console.log(chalk.green('Hello %s'), name);
  5. //=> 'Hello Sindre'
  6. ```

API


### chalk.`