ANSI color library for use in terminals, CI environments, and Chromium-based browsers.
Ansis is focused on small size and speed while providing rich functionality and handling edge cases.
// Named imports - for cleaner, more readable code import { red, cyan, bold, hex, rgb } from 'ansis'; // Clean chained syntax with template literals - no extra parentheses console.log(bold.bgRed` FAIL `); // Nested templates - no string concatenation needed console.log(red`โ Error: ${cyan`file.js`} not found`); // Truecolor: hex and rgb console.log(hex('#FF75D1').bold`Pink`); console.log(rgb(224, 17, 95).italic`Ruby`);
๐ Getting Started ๐ Ansis vs styleText() ๐ Migrating from โ๏ธ Alternatives โ
Compare features
๐ป CLI Environment ๐งช CLI Testing โ๏ธ Compatibility ๐งTroubleshooting
๐ Upgrading to v4 ยท New features ยท Breaking changes
โญ๏ธ Star History
๐จ Colors - 16 ยท 256 ยท Truecolor (hex/rgb) ยท Named colors (orange, pink ...)
โ๏ธ Syntax - Chained ยท Template literals ยท Nested templates
โ๏ธ Works - ESM ยท CJS ยท TS ยท Node 10+ ยท Bun ยท Deno ยท CI ยท Chromium browsers
๐ง Smart - Auto color detection ยท Fallback (truecolor โ 256 โ 16 โ b&w) ยท NO_COLOR ยท FORCE_COLOR
๐ฆ Tiny - 5.8 kB ยท Drop-in replacement for Chalk (44 kB)
Ansis is the fastest when using 2 or more styles, which is the common real-world use case.
| Library | 1 style | 2+ styles |
|---|---|---|
ansis |
60M ops/sec | ๐ 60M ops/sec |
picocolors |
๐ 109M ops/sec | 58M ops/sec |
chalk |
55M ops/sec | 47M ops/sec |
util.styleText |
0.5M ops/sec | 0.5M ops/sec |
๐จ Colors & Styles
- ANSI styles:
dimbolditalicunderline...strikethrough - ANSI 16 colors:
redredBrightbgRedbgRedBright... - ANSI 256 colors:
fg(n)bg(n) - Truecolor:
hex('#FF75D1')rgb(224, 17, 95)bgHex()bgRgb() - Named truecolors:
orange()bgPink()... - viaextend()
โ๏ธ Syntax
- Chained styles:
red.bold.underline- no nested calls likered(bold(underline())) - Template literals:
red`text`- no parentheses - Nested templates:
red`Error: ${cyan`file.js`} not found`- no string concatenations
๐ ๏ธ Utilities
- Strip ANSI codes:
ansis.strip(red('text'))โ plain'text' - Hyperlink:
blue.link('https://...', 'Click here'),link('https://...') - Raw escape codes:
open/close-`${red.open}Error${red.close} file not found`
๐ป Environment
- Auto color detection + Fallback: Truecolor โ 256 โ 16 โ b&w
- ENV variables:
NO_COLORFORCE_COLORTERMCOLORTERM - CLI flags:
--no-color--color - CLI testing: force color levels in tests
โ๏ธ Compatibility
- ESM ยท CJS ยท TypeScript ยท Bun ยท Deno ยท Next.js ยท CI (GitHub and others)
- Chromium browsers: Chrome ยท Edge ยท Opera ยท Brave ยท Vivaldi
- Drop-in replacement for
chalkansi-colorscoloretteand others
๐ฏ You might also like
ansilight- Truecolor syntax highlighter for the terminal with 256highlight.jsthemesflaget- CLI argument parsing. A smaller (5 kB) and faster alternative toyargs-parser(85 kB)
chalk, picocolors, colorette, kleur, ansi-colors, kolorist, cli-color, colors-cli, colors.js, tinyrainbow
Since Node.js 22 supports ANSI styling natively via util.styleText(), it is recommended for simple use cases where 16 colors are enough and top performance is not critical.
See styleText() limitations.
โ Compare features ๐ Benchmarks ๐งฉ Handling edge cases
npm install ansis
For Node.js 10โ12+ use special build
npm install ansis@node10
ESM
import ansis, { red, bold, fg, hex, rgb } from 'ansis';
CJS
const ansis = require('ansis'); const { red, bold, fg, hex, rgb } = require('ansis');
All colors, styles and functions are chainable. Each color or style can be combined in any order.
import { red, bold, hex } from 'ansis'; red.bold`text`; hex('#FF75D1').bgCyan.bold`text`; bold.hex('#FF75D1').bgCyan`text`;
Omit parentheses to keep your code clean:
import { red, yellow, green } from 'ansis'; red`Error`; // no parentheses red`Red ${yellow`Yellow ${green`Green`} Yellow`} Red`; // deep nested templates
Escape sequences work exactly as in regular strings:
red`Hello\nWorld`; // two lines in red red`prev\\next`; // one line: prev\next
dim bold italic underline strikethroughinverse visible hidden reset
There are 16 basic colors: 8 standard and 8 bright variants.
- Foreground:
fg(code)-chalk.ansi256(code)equivalent - Background:
bg(code)-chalk.bgAnsi256(code)equivalent
import { bold, fg, bg } from 'ansis'; fg(96)`Bright Cyan`; bg(105).fg(96)`Cyan text on magenta background`; bold.fg(96).underline`Bold underline Bright Cyan`;
See ANSI color codes.
If a terminal supports only 16 colors then ANSI 256 colors will be interpolated into base 16 colors.
- Foreground:
hex()rgb() - Background:
bgHex()bgRgb()
import { bold, hex, rgb, bgHex } from 'ansis'; hex('#E0115F').bold`Bold Ruby`; rgb(224, 17, 95).italic`Italic Ruby`; bold.hex('#E0115F').bgHex('#96C')`Ruby text on amethyst background`;
See also named truecolors.
Ansis automatically interpolates to the best available color level.
Truecolor โ 256 colors โ 16 colors โ no colors (b&w)
Register any hex color as a named style via extend().
Background methods bg* are generated automatically.
import { Ansis } from 'ansis'; const myTheme = { orange: '#ffa500', pink: '#ffc0cb', }; const color = new Ansis().extend(myTheme); color.orange.bold`orange bold`; // extended first in chain color.bgOrange`orange background`; // auto-generated bg tag color.pink`pink foreground`; color.bgPink`pink background`; // auto-generated bg tag color.red`built-in red still works`; // built-in remains intact
Warning
For extended styles, create a new Ansis instance and call extend() on it.
Tip
For all CSS named colors use css-color-names package.
Example: extend with CSS color names
import { Ansis } from 'ansis'; import colorNames from 'css-color-names'; // { pink: '#ffc0cb', orange: '#ffa500', ... } const color = new Ansis().extend(colorNames); color.pink('Pink foreground'); color.bgPink('Pink background'); // auto-generated bg
Tip
Need help picking a color name? Try the Name that Color tool - paste a hex and get its closest color name.
OSC 8 hyperlinks are now supported by many terminal emulators.
Use link(url, text?) to create a terminal hyperlink. If text is omitted, the URL itself is used as the visible label.
link(url, text)- link URL + optional link textlink(url)- URL as both target and text
import { blue, link } from 'ansis'; link('https://example.com'); // URL and text are the same blue.underline.link('https://example.com', 'Click here');
Important
Call link() last in the chain:
blue.underline.link(...); // โ blue.link(...).underline; // โ
Warning
In unsupported terminals text is shown without a link.
Ansis automatically detects the supported color level:
0โ No color1โ Basic ANSI (16 colors)2โ Extended ANSI (256 colors)3โ Truecolor (16 million colors)
Check the detected level at runtime:
import ansis from 'ansis'; console.log(ansis.level); // 0 | 1 | 2 | 3 console.log(ansis.isSupported()); // true -> level > 0 (at least 16 colors supported)
Create an Ansis instance directly when you need to override color detection:
import { Ansis } from 'ansis'; const noColor = new Ansis(0); // always plain text, no ANSI codes const basicColor = new Ansis(1); // 16 colors; hex/rgb fall back to nearest const autoColor = new Ansis(); // auto-detect using globalThis console.log(noColor.red`foo`); // plain text, no ANSI codes console.log(basicColor.hex('#FFAB40')`Orange`); // falls back to yellowBright
The constructor also accepts a mock globalThis object to control auto-detection in custom runtimes:
const color = new Ansis({ process: { env: { FORCE_COLOR: '1' }, // COLORTERM, TERM, CI, NO_COLOR, FORCE_COLOR argv: ['node', 'app.js'], // --no-color, --color stdout: { isTTY: false }, platform: 'linux', }, }); console.log(color.level); // 1
Example: disable colors via custom CLI flag, e.g. --save-to-log
import { Ansis } from 'ansis'; /** * @param {boolean} noColors Disable colors for non-terminal output. * @return {Ansis} Default or custom instance */ function safeAnsis(noColors) { return noColors ? new Ansis(0) // disable colors : new Ansis(); // auto/detect color support } // handle a special CLI flag to disable colors const ansis = safeAnsis(process.argv.includes('--save-to-log'))
Ansis detects color support from the runtime environment in this order:
-
Chromium browser-like runtimes
- detected first -> truecolor
-
Dumb terminal
TERM=dumb-> no colors
-
COLORTERMtruecoloror24bit-> truecoloransi256-> 256 colorsansi-> 16 colors
-
CI environment (not TTY)
- GitHub Actions -> truecolor
- other CI environments -> 16 colors
-
Windows
- Windows terminals since Windows 10 build 14931 (released 2016) -> truecolor
-
256-color terminals
- known 256-color terminals -> 256 colors
-
Fallback
- unknown terminals -> 16 colors
Colors are enabled only for a TTY, CI, and the PM2 and Next.js runtimes.
Output to a pipe or a file has no colors, even when COLORTERM is set.
To explicitly enable colors in non-TTY environments, use FORCE_COLOR.
Supported terminals and CI environments
| Terminal | ANSI 16 colors |
ANSI 256 colors |
True Color |
env. TERM |
env. COLORTERM |
Specifically ENV variables |
|---|---|---|---|---|---|---|
| Azure CI | โ | โ | โ | TF_BUILD AGENT_NAME |
||
| GitHub CI | โ | โ | โ | CI, GITHUB_ACTIONS | ||
| GitTea CI | โ | โ | โ | CI, GITEA_ACTIONS | ||
| GitLab CI | โ | โ | โ | CI, GITLAB_CI | ||
| Travis CI | โ | โ | โ | TRAVIS | ||
| PM2 not isTTY |
โ [^1] | โ [^1] | โ [^1] | PM2_HOME pm_id |
||
| Next.js edge runtime |
โ [^1] | โ [^1] | โ [^1] | NEXT_RUNTIME='edge' NEXT_RUNTIME='experimental-edge' |
||
| JetBrains TeamCity >=2020.1.1 |
โ | โ | โ | TEAMCITY_VERSION | ||
| JetBrains IDEA | โ | โ | โ | xterm-256color | TERMINAL_EMULATOR='JetBrains-JediTerm' | |
| VS Code | โ | โ | โ | xterm-256color | truecolor | |
| Windows Terminal |
โ | โ | โ [^2] | |||
| Windows PowerShell |
โ | โ | โ [^2] | |||
| macOS Terminal | โ | โ | โ | xterm-256color | ||
| iTerm | โ | โ | โ | xterm-256color | truecolor | |
| Kitty | โ | โ | โ | xterm-kitty | truecolor | |
| KDE Konsole | โ | โ | โ | xterm-256color | truecolor |
- ^1 Colors supported depends on actual output destination.
TERM=dumbdisables colors before PM2 and Next.js detection. - ^2: The Windows terminal supports true color since Windows 10 revision 14931 (2016-09-21).
See also:
Ansis detects color support automatically, but you can override it with environment variables and CLI flags.
Set to any non-empty value (1, true) to disable color output (see no-color.org):
NO_COLOR=1 node app.js
Force or override color support via environment variable (see force-color.org).
| Value | Behavior |
|---|---|
0 or false |
Disable colors (level 0) |
1 |
Force 16 colors (level 1) |
2 |
Force 256 colors (level 2) |
3 |
Force truecolor (level 3) |
true or any other string |
Detect the level for any output, fallback to 16 colors |
Important
Ansis differs from the original force-color.org rule in a few cases:
-
FORCE_COLOR=0disables colors.
This matches Node.js, but the original force-color.org rule enables colors when the value is a non-empty string, regardless of the actual value. -
FORCE_COLOR=trueand an empty value enable colors.
This matches Node.js, but the original force-color.org rule ignores an empty string.
TERM identifies the current terminal type and is used during auto-detection.
| Value | Behavior |
|---|---|
dumb |
Disable colors during auto-detection |
*-256color |
Enable 256 colors when no earlier auto-detection check matched |
| other known terminal names | Enable 16 colors when the output environment is supported |
Examples of TERM values with 256-color support: screen-256color, xterm-256color, rxvt-256color, putty-256color, mintty-256color, linux-256color, tmux-256color, ansi-256color.
TERM=dumb has higher priority than COLORTERM, CI detection, PM2, Next.js edge runtime, and platform-specific defaults.
To explicitly override auto-detection, use FORCE_COLOR.
COLORTERM hints at how many colors a terminal can show. It does not turn colors on by itself. Ansis uses it to choose the color level when color output is supported.
| Value | Level |
|---|---|
ansi |
16 colors (level 1) |
ansi256 |
256 colors (level 2) |
truecolor or 24bit |
Truecolor (level 3) |
Some terminals (iTerm, VS Code, xterm-kitty, KDE Konsole, and others) set COLORTERM=truecolor in addition to a TERM value that only advertises 256-color support, for example TERM=xterm-256color.
Pass --no-color or --color directly to your script:
./app.js # auto-detect ./app.js --no-color # disable colors ./app.js --color # enable colors (useful when piping output)
Note
If multiple CLI color flags are present, the last one wins:
--color --no-color->--no-color--no-color --color->--color
node app.js # auto-detect node app.js > log.txt # no colors (non-TTY) NO_COLOR=1 node app.js # force off FORCE_COLOR=0 node app.js # force off FORCE_COLOR=1 node app.js > log.txt # force 16 colors FORCE_COLOR=2 node app.js > log.txt # force 256 colors FORCE_COLOR=3 node app.js > log.txt # force truecolor FORCE_COLOR=true node app.js > log.txt # detect the level, fallback to 16 colors node app.js --no-color # disable via flag node app.js --color > log.txt # detect the level via flag, fallback to 16 colors
Ansis and Chalk add a style break at each new line to correctly display multi-line text.
However, Picocolors doesn't handle this case.
ansis.bgRed('\n ERROR \n') + ansis.cyan('The file not found!') // โ chalk.bgRed('\n ERROR \n') + chalk.cyan('The file not found!') // โ pico.bgRed('\n ERROR \n') + pico.cyan('The file not found!') // โ
Only Ansis handles this very useful use case.
ansis.red`R ${ansis.green`G ${ansis.blue`B`} G`} R` // โ chalk.red`R ${chalk.green`G ${chalk.blue`B`} G`} R` // โ pico.red`R ${pico.green`G ${pico.blue`B`} G`} R` // โ
Compare how different libraries handle various input arguments in their functions.
ansis.red() // โ '' chalk.red() // โ '' pico.red() // โ \e[31mundefined\e[39m ansis.red(undefined) // โ '' chalk.red(undefined) // โ \e[31mundefined\e[39m pico.red(undefined) // โ \e[31mundefined\e[39m ansis.red(null) // โ '' chalk.red(null) // โ \e[31mnull\e[39m pico.red(null) // โ \e[31mnull\e[39m ansis.red('') // โ '' chalk.red('') // โ '' pico.red('') // โ \e[31m\e[39m ansis.reset() // โ \e[0m chalk.reset() // โ '' pico.reset() // โ \e[0mundefined\e[0m
| Library | c.reset() |
c.red() |
c.red(undefined) |
c.red(null) |
c.red('') |
|---|---|---|---|---|---|
ansis |
โ
\e[0m |
โ
'' |
โ
'' |
โ
'' |
โ
'' |
chalk |
โ'' |
โ
'' |
โ'undefined' |
โ'null' |
โ
'' |
picocolors |
โundefined |
โ'undefined' |
โ'undefined' |
โ'null' |
โ'ESC' |
tinyrainbow |
โundefined |
โ'undefined' |
โ'undefined' |
โ'null' |
โ'ESC' |
colorette |
โ'' |
โ
'' |
โ
'' |
โ'null' |
โ
'' |
kleur |
โ[object] |
โ[object] |
โ[object] |
โ'null' |
โ'ESC' |
ansi-colors |
โ'' |
โ
'' |
โ
'' |
โ
'' |
โ
'' |
kolorist |
โundefined |
โ'undefined' |
โ'undefined' |
โ'null' |
โ'ESC' |
colors.js |
โ'' |
โ
'' |
โ'undefined' |
โ'null' |
โ
'' |
cli-color |
โ- |
โ'ESC' |
โ'ESC' |
โ'ESC' |
โ'ESC' |
colors-cli |
โ- |
โ Error |
โ'undefined' |
โ'null' |
โ'ESC' |
- โ
''- Returns an empty string without ANSI escape codes. This is the correct and expected behavior. - โ
\e[0m- Returns the reset escape code. - โ
'ESC'- Returns an empty string containing ANSI escape codes, e.g.,\e[31m\e[39m. - โ
'undefined'- Returns the styled stringundefined. - โ
'null'- Returns the styled stringnull. - โ
[object]- Returns an object of the library instance. - โ
-- The feature is not supported. - โ
Error- Causes a fatal error.
Other arguments are correctly handled by all libraries:
c.red(0) // '0' in red c.red(false) // 'false' in red c.red(true) // 'true' in red c.red(5/'1px') // 'NaN' in red c.red(1/0) // 'Infinity' in red
Since Node v22, the built-in util.styleText()
has been officially introduced, supporting standard modifiers - the basic 16 colors and styles.
Ansis
โ
Node v10+
โ
Chromium-based browsers and Safari
styleText
โ
Node v22+ (native)
โ No browser support (Node only)
In practical benchmarks, styleText() is 100x slower than Ansis:
ansis.red('text'); // 59.646.465 ops/sec styleText('red', 'text'); // 579.832 ops/sec
See full benchmarks.
Ansis
- Auto-detects terminal, TTY, CI and browser color support with automatic fallback
- Supports
NO_COLORFORCE_COLORCOLORTERM--no-color--color ansis.levelreturns the detected color level
styleText
- Auto-detects terminal color support
- Supports only
NO_COLORFORCE_COLORNODE_DISABLE_COLORS
import { green } from 'ansis'; import { styleText } from 'node:util'; green`Success!`; // ansis styleText('green', 'Success!'); green.bold`Success!`; // ansis styleText(['green', 'bold'], 'Success!');
import { red, cyan } from 'ansis'; import { styleText } from 'node:util'; red`Error: ${cyan.bold`file.js`} not found!`; // ansis styleText('red', `Error: ${styleText(['cyan', 'bold'], 'file.js')} not found!`);
Ansis: hex() rgb()
styleText: Limited to 16 ANSI colors.
Check the minimum version of your tool required for compatibility with the latest Ansis.
| Tool | Version | Compatibility | Supports |
|---|---|---|---|
| Node.js | v14+ | โ Full support | CJS, ESM |
| Deno | v2.0+ | โ Full support | CJS, ESM |
| TypeScript/tsc | v5.0+ | โ Full support | CJS, ESM |
| esbuild | v0.8+ | โ Full support | CJS, ESM |
| swc | v1.2+ | โ Full support | CJS, ESM, FAUX |
| tsup | v4.0+ | โ Full support | CJS, ESM, FAUX |
| tsx | v3.0+ | โ Full support | CJS, ESM |
| Rollup | v2.0+ | โ Full support | CJS, ESM |
| Rolldown | v1.0.0-beta.8+ | โ Full support | CJS, ESM |
| Vite | v2.5+ | โ Full support | ESM |
| Turbo | v1.0+ | โ Full support | CJS, ESM |
| Webpack | v5.0+ | โ Full support | CJS, ESM |
Supports:
- CJS: CommonJS module support.
- ESM: ECMAScript module support.
- FAUX: Fake or non-standard approach to module resolution (seen in swc).
| Browser | Version | Colors Supported |
|---|---|---|
| Chrome | v20+ | TrueColor (16M) |
| Safari | v10+ | TrueColor (16M) |
| Edge | v12+ | TrueColor (16M) |
| Opera | v12+ | TrueColor (16M) |
| Brave | v1.0+ | TrueColor (16M) |
| Vivaldi | v1.0+ | TrueColor (16M) |
Warning
Firefox doesn't natively support ANSI codes in the developer console.
If you find this useful, please โญ๏ธ the repo.




















