Chalk is a terminal string styling library that provides a fluent API for applying colors, text formatting, and visual effects to console output. It features automatic terminal color support detection, chainable styling methods, and supports advanced color models including RGB, HEX, and 256-color palettes.
npm install chalkimport chalk from 'chalk';For multiple imports:
import chalk, { Chalk, chalkStderr } from 'chalk';Named imports:
import {
Chalk,
chalkStderr,
supportsColor,
supportsColorStderr,
modifierNames,
foregroundColorNames,
backgroundColorNames,
colorNames
} from 'chalk';import chalk from 'chalk';
// Basic styling
console.log(chalk.blue('Hello world!'));
// Chain multiple styles
console.log(chalk.blue.bgRed.bold('Hello world!'));
// Advanced colors
console.log(chalk.rgb(123, 45, 67)('RGB color'));
console.log(chalk.hex('#DEADED')('Hex color'));
console.log(chalk.ansi256(201)('256-color'));
// Create themes
const error = chalk.bold.red;
const warning = chalk.hex('#FFA500');
console.log(error('Error!'));
console.log(warning('Warning!'));Chalk is built around several key components:
Chalk class for creating custom instances with specific configurationsThe main chalk instance provides the primary interface for styling terminal text.
/**
* Main Chalk instance - default export
* Applies accumulated styles to text input
*/
const chalk: ChalkInstance;
// Usage: chalk(...text: unknown[]): string
// All styling methods return the same instance for chainingText formatting modifiers including bold, italic, underline, and color inversion.
// Modifiers (all return ChalkInstance for chaining)
readonly reset: ChalkInstance;
readonly bold: ChalkInstance;
readonly dim: ChalkInstance;
readonly italic: ChalkInstance;
readonly underline: ChalkInstance;
readonly overline: ChalkInstance;
readonly inverse: ChalkInstance;
readonly hidden: ChalkInstance;
readonly strikethrough: ChalkInstance;
readonly visible: ChalkInstance;16 standard terminal colors for both foreground and background styling.
// Foreground colors (all return ChalkInstance for chaining)
readonly black: ChalkInstance;
readonly red: ChalkInstance;
readonly green: ChalkInstance;
readonly yellow: ChalkInstance;
readonly blue: ChalkInstance;
readonly magenta: ChalkInstance;
readonly cyan: ChalkInstance;
readonly white: ChalkInstance;
// Bright variants
readonly blackBright: ChalkInstance; // Also available as: gray, grey
readonly redBright: ChalkInstance;
readonly greenBright: ChalkInstance;
readonly yellowBright: ChalkInstance;
readonly blueBright: ChalkInstance;
readonly magentaBright: ChalkInstance;
readonly cyanBright: ChalkInstance;
readonly whiteBright: ChalkInstance;Background color styling using the same color palette as foreground colors.
// Background colors (all return ChalkInstance for chaining)
readonly bgBlack: ChalkInstance;
readonly bgRed: ChalkInstance;
readonly bgGreen: ChalkInstance;
readonly bgYellow: ChalkInstance;
readonly bgBlue: ChalkInstance;
readonly bgMagenta: ChalkInstance;
readonly bgCyan: ChalkInstance;
readonly bgWhite: ChalkInstance;
// Bright background variants
readonly bgBlackBright: ChalkInstance; // Also available as: bgGray, bgGrey
readonly bgRedBright: ChalkInstance;
readonly bgGreenBright: ChalkInstance;
readonly bgYellowBright: ChalkInstance;
readonly bgBlueBright: ChalkInstance;
readonly bgMagentaBright: ChalkInstance;
readonly bgCyanBright: ChalkInstance;
readonly bgWhiteBright: ChalkInstance;RGB, HEX, and 256-color support for terminals that support advanced color modes.
// Advanced color methods (all return ChalkInstance for chaining)
rgb(red: number, green: number, blue: number): ChalkInstance;
hex(color: string): ChalkInstance;
ansi256(index: number): ChalkInstance;
bgRgb(red: number, green: number, blue: number): ChalkInstance;
bgHex(color: string): ChalkInstance;
bgAnsi256(index: number): ChalkInstance;Creating custom Chalk instances and managing color support levels.
// Constructor for creating custom instances
const Chalk: new (options?: Options) => ChalkInstance;
interface Options {
readonly level?: ColorSupportLevel; // 0-3, overrides auto-detection
}
type ColorSupportLevel = 0 | 1 | 2 | 3;Built-in terminal color capability detection and metadata.
const supportsColor: ColorInfo;
const supportsColorStderr: ColorInfo;
const chalkStderr: ChalkInstance;
type ColorInfo = ColorSupport | false;
interface ColorSupport {
level: ColorSupportLevel;
hasBasic: boolean; // Basic 16 colors supported
has256: boolean; // ANSI 256 colors supported
has16m: boolean; // Truecolor 16M colors supported
}Legacy aliases maintained for backwards compatibility.
// Deprecated: Use corresponding *Names exports instead
const modifiers: readonly string[]; // Use modifierNames
const foregroundColors: readonly string[]; // Use foregroundColorNames
const backgroundColors: readonly string[]; // Use backgroundColorNames
const colors: readonly string[]; // Use colorNames
// Deprecated type aliases
type Modifiers = ModifierName; // Use ModifierName
type ForegroundColor = ForegroundColorName; // Use ForegroundColorName
type BackgroundColor = BackgroundColorName; // Use BackgroundColorName
type Color = ColorName; // Use ColorNameinterface ChalkInstance {
// Function call - applies accumulated styles to text
(...text: unknown[]): string;
// Color support level (0-3)
level: ColorSupportLevel;
// All style properties and methods are available for chaining
}
// Color support levels
type ColorSupportLevel = 0 | 1 | 2 | 3;
// 0: All colors disabled
// 1: Basic 16 colors support
// 2: ANSI 256 colors support
// 3: Truecolor 16 million colors support
// Style name arrays for validation/enumeration
const modifierNames: readonly ModifierName[];
const foregroundColorNames: readonly ForegroundColorName[];
const backgroundColorNames: readonly BackgroundColorName[];
const colorNames: readonly ColorName[];
// Type definitions for style names
type ModifierName = 'reset' | 'bold' | 'dim' | 'italic' | 'underline' |
'overline' | 'inverse' | 'hidden' | 'strikethrough' | 'visible';
type ForegroundColorName = 'black' | 'red' | 'green' | 'yellow' | 'blue' |
'magenta' | 'cyan' | 'white' | 'blackBright' | 'redBright' | 'greenBright' |
'yellowBright' | 'blueBright' | 'magentaBright' | 'cyanBright' | 'whiteBright' |
'gray' | 'grey';
type BackgroundColorName = 'bgBlack' | 'bgRed' | 'bgGreen' | 'bgYellow' |
'bgBlue' | 'bgMagenta' | 'bgCyan' | 'bgWhite' | 'bgBlackBright' | 'bgRedBright' |
'bgGreenBright' | 'bgYellowBright' | 'bgBlueBright' | 'bgMagentaBright' |
'bgCyanBright' | 'bgWhiteBright' | 'bgGray' | 'bgGrey';
type ColorName = ForegroundColorName | BackgroundColorName;