docs/server/reference/colors.md
Wraps text with ANSI escape codes for terminal colors and styles.
import { colors } from 'es-toolkit/server';
console.log(colors.red('error'));
console.log(colors.bold(colors.cyan('hello')));
colors is an object that exposes ANSI styling functions. Each function takes a string and returns a string wrapped with the corresponding open and close codes.
import { colors } from 'es-toolkit/server';
colors.red('error');
colors.bold('emphasized');
colors.underline(colors.cyan('link'));
The functions are tree-shake friendly: only the styles you actually reference end up in your bundle.
reset, bold, dim, italic, underline, inverse, hidden, strikethrough
black, red, green, yellow, blue, magenta, cyan, white, gray
blackBright, redBright, greenBright, yellowBright, blueBright, magentaBright, cyanBright, whiteBright
bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite
bgBlackBright, bgRedBright, bgGreenBright, bgYellowBright, bgBlueBright, bgMagentaBright, bgCyanBright, bgWhiteBright
The extended-color helpers are curried — pass the color first, then the text:
ansi256(code) / bgAnsi256(code) — 8-bit (256-color) palette, code in 0–255.rgb(r, g, b) / bgRgb(r, g, b) — 24-bit truecolor.hex(color) / bgHex(color) — 24-bit truecolor parsed from #RGB, #RRGGBB, or the same without #.import { colors } from 'es-toolkit/server';
const orange = colors.rgb(255, 99, 71);
console.log(orange('hello'));
console.log(colors.hex('#f06')('hello'));
console.log(colors.bgAnsi256(21)('hello'));
Nested calls re-open the outer style after the inner one closes, so the surrounding color survives:
import { colors } from 'es-toolkit/server';
console.log(colors.red(`error: ${colors.underline('not found')}, please retry`));
Background helpers additionally re-open across line breaks, since most terminals reset the background at every newline:
import { colors } from 'es-toolkit/server';
console.log(colors.bgYellow('line one\nline two'));
Helpers that return a styling function (ansi256, bgAnsi256, rgb, bgRgb, hex, bgHex) return a ColorFunction:
import type { ColorFunction } from 'es-toolkit/server';
type ColorFunction = (text: string) => string;
colors does not detect whether the terminal supports color — every call emits ANSI escape codes unconditionally. If you need to honor NO_COLOR, FORCE_COLOR, TTY detection, or CI environments, gate the call yourself.