Chalk adds ANSI colors and text styles to strings in Node.js CLI tools. It offers a chainable API with automatic color-level detection.
Overview
Chalk is a JavaScript package for coloring and styling text printed to terminals. It targets command-line scripts, build tools, and libraries that want readable log output without hand-writing escape codes.
The API chains modifiers, foreground and background colors, and underline colors. You can nest styles, compose custom themes, and pass multiple arguments that are joined with spaces. It detects how much color the terminal supports, from basic 16 colors through 256-color and truecolor RGB or hex values.
A separate stderr instance mirrors stdout detection rules for streams that log errors differently. Level can be set globally or on a new instance, and environment variables or flags can force color on or off. Chrome’s developer console is noted as supporting ANSI escapes since a recent release.
The package ships with no runtime dependencies and does not patch String.prototype. It exposes name lists for modifiers and colors, which helps when wrapping or validating user input.
Key features
- Chainable ANSI styling API
- 16, 256, and truecolor support
- Auto-detects terminal color level
- Nested and composable themes
- Separate chalkStderr instance
- Hex, RGB, and ansi256 helpers
- No String.prototype extension
- Zero package dependencies
Use cases
- Coloring CLI script output
- Highlighting errors in terminal logs
- Theming developer tool messages
- Validating user-chosen style names
- Formatting console output in Node