colors â
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')));Usage â
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.
Modifiers â
reset, bold, dim, italic, underline, inverse, hidden, strikethrough
Foreground colors â
black, red, green, yellow, blue, magenta, cyan, white, gray
Bright foreground colors â
blackBright, redBright, greenBright, yellowBright, blueBright, magentaBright, cyanBright, whiteBright
Background colors â
bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite
Bright background colors â
bgBlackBright, bgRedBright, bgGreenBright, bgYellowBright, bgBlueBright, bgMagentaBright, bgCyanBright, bgWhiteBright
Extended colors â
The extended-color helpers are curried â pass the color first, then the text:
ansi256(code)/bgAnsi256(code)â 8-bit (256-color) palette,codein0â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'));Composition â
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'));Types â
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;Notes â
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.

