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.
Note
Migration guide to v4, note the new features and breaking changes.
- Supports ESM, CommonJS, TypeScript, Bun, Deno, Next.JS
- Works in Chromium-based browsers: Chrome, Edge, Opera, Brave, Vivaldi
- Default and named imports:
import ansis, { red, bold, dim } from 'ansis' - Chained syntax:
red.bold.underline('text') - Nested tagged template strings:
red`Error: ${blue`file.js`} not found!` - ANSI styles:
dimbolditalicunderlinestrikethrough - ANSI 16 colors:
red,redBright,bgRed,bgRedBright, ... - ANSI 256 colors via methods:
fg(num),bg(num) - Truecolor via methods:
rgb(r,g,b),bgRgb(r,g,b),hex('#rrggbb'),bgHex('#rrggbb') - Named truecolors, like orange, pink, tomato, seegreen, etc.:
ansis.orange(),ansis.bgOrange(), ... - Auto-detects color support: Truecolor, 256 colors, 16 colors, no colors
- Automatic fallback: Truecolor → 256 colors → 16 colors → no colors
- Raw ANSI escape codes:
`File ${red.open}not found${red.close} in directory` - Strip ANSI escape codes with
ansis.strip() - Supports ENV variables and flags:
NO_COLOR,FORCE_COLOR,COLORTERM,--no-color,--color - Reliable CLI testing with forced color levels: no color, 16, 256 or Truecolor
- Drop-in replacement for
chalkansi-colorscolorettepicocolorsand others alternatives
🎯 You might also like
flaget- a smaller (5 kB) and faster alternative toyargs-parser(85 kB) for CLI argument parsing.
Install the default version, optimized for modern environments.
npm install ansisInstall this special build only if you require compatibility with Node.js v10–v12 or newer.
npm install ansis@node10import ansis, { red, bold, fg, hex, rgb } from 'ansis';
console.log(ansis.bold('file.txt'));
console.log(red`Error: ${bold.cyan(file)} not found!`);
console.log(bold.bgRed`ERROR`);
console.log(fg(208)`Orange`);
console.log(rgb(224, 17, 95)`Ruby`);
console.log(hex('#FF75D1').bold.underline('Pink'));
console.log(ansis.strip(red('Text'))); // Output plain text without ANSI codesThe most popular ANSI libraries, similar to Ansis:
chalk, picocolors, colorette, kleur, ansi-colors, kolorist, cli-color, colors-cli, colors.js, tinyrainbow
✅ Compare features 🧩 Handling edge cases 📦 Compare package sizes 📊 Benchmarks
As of 2025, two of the smallest and fastest ANSI libraries are Ansis and Picocolors. Both are recommended by the e18e community as modern replacements for older, larger libraries.
The package size in node_modules directory:
picocolors: 6.37 kB (not minimized) - A micro library with basic features.аnsis: 5.7 kB (minimized) - A powerful library with a rich set of features.chalk: 44.2 kB (not minimized) - Provides similar functionality to Ansis.
picocolors: The fastest when applying a single style (e.g.,red) only.аnsis: The fastest when applying two or more styles (e.g.,red+bgWhite).chalk: Slower than both Ansis and Picocolors in all use cases.
See Benchmarks.
Caution
Picocolors is the fastest in a simple micro-benchmark because it doesn't handle important edge cases. However, that benchmark doesn't reflect real-world usage. In more complex and realistic benchmarks, Ansis performance is comparable to Picocolors, or even faster.
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[0mSee more details about handling input arguments in various libraries.
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` // ❌As of 2025, only Ansis, Chalk, and Picocolors are actively maintained, unlike many other libraries:
colorette: Last updated 2 years agoansi-colors: Last updated 3 years agokleur: Last updated 3 years agocolors.js: Last updated 2 years agocli-color: Last updated 1 year agocolors-cli: Last updated 1 year ago
-
If you only use a single style, e.g.,
red('foo'), Picocolors is the best solution. -
However, if you need more, like combining multiple styles (e.g.,
red+bold+bgWhite) with chained syntax, named color imports, 256 colors or Truecolor then Ansis is the better choice.
- Does support for ESM or CJS matter?
- ✅ Ansis:
ESMandCJS - ☑️ Picocolors:
CJSonly - ☑️ Chalk:
ESMonly
- ✅ Ansis:
- Does it matter the unpacked size?
- Does it matter if a library performs ~60 million or ~100 million ops/sec when outputting to the terminal?
Spoiler: All libraries are more than fast enough.
- ✅ Picocolors
- ✅ Ansis
- ✅ Chalk
- Does support for ANSI 256 colors or Truecolor with fallback matter?
- ✅ Ansis
- ✅ Chalk
- ❌ Picocolors
- Does handling edge cases matter?
- ✅ Ansis
- ☑️ Chalk
- ❌ Picocolors
- Does keeping your code clean and readable matter?
- ✅ Ansis (default and named import, chained syntax, nested template strings)
- ✅ Chalk (default import, chained syntax)
- ☑️ Picocolors (default import, nested calls)
Explore the list of features, package sizes, and benchmarks compared to similar libraries.
Tip
To keep your code clean and readable:
- Use the chained syntax provided by libraries like
ansisandchalk - Avoid nested calls, as they are much slower and less readable than the chained syntax.
import ansis, { red, green, cyan } from 'ansis' // supports both default and named imports
import chalk from 'chalk' // doesn't support named import
import pico from 'picocolors' // doesn't support named import
ansis.red('Error') // ansis: slower than picocolors
chalk.red('Error') // chalk: slower than ansis
pico.red('Error') // picocolors: fastest
red.bold.bgWhite`Error` // ansis: fastest, clean
chalk.red.bold.bgWhite('Error') // chalk: a bit slower, short
pico.red(pico.bold(pico.bgWhite('Error'))) // picocolors: 2x slower than ansis, verbose
green`Create ${blue.bold`React`} app.` // ansis
chalk.green(`Create ${chalk.blue.bold('React')} app.`) // chalk
pico.green(`Create ${pico.blue(pico.bold('React'))} app.`) // picocolorsTip
Ansis supports nested template strings, so you can colorize text without using parentheses.
Since Node v22, the built-in util.styleText()
has been officially introduced, supporting standard modifiers - the basic 16 colors and styles.
Ansis
✅ Works on Node v10+
✅ Works in Chromium-based browsers and Safari (useful for shared utils)
styleText
✅ Native since Node v22+
❌ Node only - it doesn't work in browsers
In practical benchmarks, styleText() is dramatically slower, 100x slower than Ansis:
styleText('red', 'text'); // 579.832 ops/sec
ansis.red('text'); // 59.646.465 ops/secSee the benchmark below.
Ansis
- Detects terminal, TTY, CI, or browser color capability and automatically falls back to the supported level (truecolor → 256 → 16 → no color).
- Supports common flags and environment variables:
NO_COLOR,FORCE_COLOR,COLORTERM,--no-color,--color. - The property
ansis.levelreturns supported color level.
styleText
- Detects terminal color support automatically.
- Supports only environment variables:
NO_COLOR,FORCE_COLOR,NODE_DISABLE_COLORS.
Ansis has a compact and elegant syntax:
import ansis, { green } from 'ansis';
console.log(ansis.green('Success!'));
// or even shortly using named import
console.log(green`Success!`);
console.log(green.bold`Success!`);The same example with styleText is more verbose:
const { styleText } = require('node:util');
console.log(styleText('green', 'Success!'));
console.log(styleText(['green', 'bold'], 'Success!'));Ansis keeps your code short and readable:
import { red, cyan } from 'ansis';
console.log(red`Error: ${cyan.bold`file.js`} not found!`);With Ansis tagged templates, your code reads like prose - clear messages instead of verbose, nested calls and brackets.
Using styleText becomes awkward and verbose for nested or combined styles:
const { styleText } = require('node:util');
console.log(styleText('red', `Error: ${styleText(['cyan', 'bold'], 'file.js')} not found!`));Ansis
- Supports 16-color, 256-color, and truecolor output.
- Truecolor methods
hex()andrgb():console.log(ansis.hex('#ffa500')('orange text')); console.log(ansis.rgb(255, 165, 0)('orange text'));
- Supports named truecolors (via extension):
console.log(ansis.orange('Orange foreground')); console.log(ansis.bgPink('Pink background'));
styleText
- Limited to the 16 ANSI colors and standard styles.
- No support for hex, rgb, or named truecolors.
Ansis includes d.ts type definitions for seamless TypeScript integration.
Color names, methods, and style chains are fully typed, enabling autocomplete and type checking in IDEs like VS Code.
If you only need basic 16 colors, simple single-style text, and Node-only,
styleText() is perfectly fine and dependency free.
Tip
You can easily migrate from picocolors, colorette or kleur to styleText.
If you need truecolor, multi-style chaining, nested templates, complex compositions, or browser compatibility,
Ansis remains the more elegant, expressive, and future-proof solution.
It's built to work reliably across a wide range of environments:
terminals, TTY, CI pipelines, and modern browsers, automatically adapting to the available color capabilities.
ESM
// Default import
import ansis from 'ansis';
// Named imports
import { red, green, bold, dim } from 'ansis';
// Default and named import in the same statement
import ansis, { red, green, bold, dim } from 'ansis';CommonJS
// Default import
const ansis = require('ansis');
// Destructuring styles
const { red, green, bold, dim } = require('ansis');Warning
CommonJS (require) does not support destructuring and default import in the same statement like ES Modules.
const ansis = require('ansis');
const { red, green, bold, dim } = ansis;
let out = red('text');
console.log(ansis.strip(out));Tip
Using template literals you can omit parentheses red(`error`) → red`error` to keep your code clean and readable.
import { cyan, red } from 'ansis';
let file = '/path/to/file.txt';
red`Error: File ${cyan(file)} not found!`;Ansis processes tagged template literals the same way as normal strings.
red('Hello\nWorld');
red`Hello\nWorld`;Output (two lines in red):
Hello
World
To preserve \n, \t, \p and similar sequences as literal, escape them with a backslash (\):
red('prev\\next');
red`prev\\next`;Output (one line in red):
prev\next
Ansis correctly renders nested tagged template strings.
import { green, red, yellow } from 'ansis';
red`Red ${yellow`Yellow ${green`Green`} Yellow`} Red`;
red`Error: ${yellow`Module ${green`ansis`} is missing!`} Installation required.`;All colors, styles and functions are chainable. Each color or style can be combined in any order.
import { red, bold, italic, hex } from 'ansis';
red.bold`text`;
hex('#FF75D1').bgCyan.bold`text`;
bold.bgHex('#FF75D1').cyan`text`;
italic.bold.yellow.bgMagentaBright`text`;dim bold italic underline strikethroughinverse visible hidden reset
There are 16 basic colors: 8 standard and 8 bright variants.
See also named truecolors.
The same ANSI codes 90 (gray) and 100 (bgGray) are named differently in various libraries.
| Library | StandardgraybgGray |
UK spellinggreybgGrey |
Spec-styleblackBrightbgBlackBright |
|---|---|---|---|
| ansis | ✅ | ❌ | ❌ |
| yoctocolors | ✅ | ❌ | ❌ |
| kolorist | ✅ | ❌ | ❌ |
| colors.js | ✅ | ✅ | ❌ |
| picocolors | ✅ | ❌ | ✅ |
| tinyrainbow | ✅ | ❌ | ✅ |
| colorette | ✅ | ❌ | ✅ |
| chalk | ✅ | ✅ | ✅ |
| ansi-colors | ✅ | ✅ | ✅ |
| kleur (8 colors) | ✅ | ✅ | - |
| cli-color | ❌ | ❌ | ✅ |
| colors-cli | ❌ | ❌ | ✅ |
| styleText | ✅ | ✅ | ✅ |
Ansis prefers the more intuitive and commonly used names gray and bgGray, avoiding redundant aliases.
256 color functions:
- Foreground:
fg(code)- equivalent tochalk.ansi256(code) - Background:
bg(code)- equivalent tochalk.bgAnsi256(code)
256 color codes:
See ANSI color codes.
If a terminal supports only 16 colors then ANSI 256 colors will be interpolated into base 16 colors.
import { bold, fg, bg } from 'ansis';
// foreground color
fg(96)`Bright Cyan`;
// background color
bg(105)`Bright Magenta`;
// function is chainable
fg(96).bold`bold Bright Cyan`;
// function is available in each style
bold.fg(96).underline`bold underline Bright Cyan`;
// you can combine the functions and styles in any order
bg(105).fg(96)`cyan text on magenta background`You can use the hex or rgb format.
Foreground function: hex() rgb()
Background function: bgHex() bgRgb()
import { bold, hex, rgb, bgHex, bgRgb } from 'ansis';
// foreground color
hex('#E0115F').bold`bold Ruby`;
hex('#96C')`Amethyst`;
rgb(224, 17, 95).italic`italic Ruby`;
// background color
bgHex('#E0115F')`Ruby`;
bgHex('#96C')`Amethyst`;
bgRgb(224, 17, 95)`Ruby`;
// you can combine the functions and styles in any order
bold.hex('#E0115F').bgHex('#96C')`ruby bold text on amethyst background`The ansis supports fallback to supported color level.
Truecolor —> 256 colors —> 16 colors —> no colors (black & white)
If you use the hex(), rgb() or ansis256() functions in a terminal not supported Truecolor or 256 colors, then colors will be interpolated.
See also fallback for named truecolors.
Ansis supports full 24-bit color via ansis.rgb(r, g, b) and ansis.hex('#rrggbb').
If you prefer named colors (e.g. orange, pink, navy, etc.)
instead of writing hex or RGB values by hand, resolve color names in your app and register them as extended styles on an Ansis instance via ansis.extend().
Then you can call e.g., color.pink() or color.bgPink() rather than using ansis.hex('#ffc0cb') or ansis.bgHex('#ffc0cb') directly.
Important
Foreground methods are created from the provided color names, and matching background methods bg* are generated automatically.
Note
To keep Ansis small, it doesn't bundle large truecolors name table.
Use any mapping package you like, e.g. css-color-names (~6 kB).
npm i css-color-namesView named colors example:
cd ./examples
npm i
npm run named-truecolorsExample (extend with all CSS color names )
import ansis from 'ansis';
import colorNames from 'css-color-names';
// `colorNames` is an object like { pink: '#ffc0cb', orange: '#ffa500', ... }
// `extend()` registers each key as a chainable style on the returned instance
const color = ansis.extend(colorNames);
// All color names are now avaliable as chainable methods on the extended instance:
console.log(color.pink('Pink foreground'));
console.log(color.bgPink('Pink background')); // auto-generated from "pink"If you prefer to keep the ansis namespace:
import { Ansis } from 'ansis';
import colorNames from 'css-color-names';
// Create a new instance and extend it with colors
const ansis = new Ansis().extend(colorNames);
console.log(ansis.pink('Pink foreground'));
console.log(ansis.bgPink('Pink background'));Of course, you can define a custom subset with only the colors you actually use.
Tip
Need help picking a color name? Try the Name that Color tool - paste a hex and get its closest color name.
Example (custom subset)
import ansis from 'ansis';
const myTheme = {
orange: '#ffa500',
pink: '#ffc0cb',
};
// Extend with only your colors
const color = ansis.extend(myTheme);
// You can still use base styles together with extended ones
const { orange, pink, bgPink, red } = color;
console.log(color.orange('orange foreground')); // extended foreground
console.log(color.bgOrange('orange background')); // extended background
console.log(orange.italic`orange italic`); // extended + base style
console.log(pink`pink foreground`); // extended as a tag
console.log(bgPink`pink background`); // extended as a tag
console.log(red('built-in red still works')); // built-in remains intactTypeScript example
import ansis, { AnsiColors } from 'ansis';
// Extends the built-in `AnsiColors` type with truecolor names
// and their auto-generated bg* color names
type AnsiColorsExtend<T extends string> = AnsiColors | T | `bg${Capitalize<T>}`;
const myTheme = {
orange: '#ffa500',
pink: '#ffc0cb',
};
// Extend and get back a typed instance (includes built-ins + your colors)
const color = ansis.extend(myTheme);
// A tiny logger that accepts both built-in and extended styles
const log = (style: AnsiColorsExtend<keyof typeof myTheme>, message: string) => {
console.log(color[style](message));
}
log('red', 'red color'); // ✅ built-in
log('bgRed', 'red background'); // ✅ built-in background
log('orange', 'orange color'); // ✅ extended
log('bgOrange', 'orange background'); // ✅ auto-generated background from extended
console.log(color.pink`pink foreground`); // ✅ extended
console.log(color.bgPink`pink background`); // ✅ auto-generated background from extended
// log('unknown', 'nope'); // ❌ TypeScript errorWarning
Order in the call chain matters. Put extended colors first in the chain:
color.orange.bold('orange bold'); // ✅ works: extended first, then built-ins
color.bold.orange('bold orange'); // ❌ won't work: extended is on a sub-chainAnsis automatically interpolates named truecolors to the highest available color level supported by the current environment. So you can safely use named truecolors anywhere without worrying about compatibility.
Example:
import ansis from 'ansis';
import colorNames from 'css-color-names';
const color = ansis.extend(colorNames);
console.log(color.orange('Text'));Output depending on terminal color support:
| Color level | Result | Example output |
|---|---|---|
| Truecolor / 24-bit | rgb(255,165,0) (orange) |
\x1b[38;2;255;165;0mText\x1b[39m |
| 256 colors | palette index 214 |
\x1b[38;5;214mText\x1b[39m |
| 16 colors | code 93 (bright yellow) |
\x1b[93mText\x1b[39m |
| No color | plain text | Text |
By default, output in the terminal console is colored, while output in a file is uncolored.
To force enable or disable colored output, you can use the NO_COLOR and FORCE_COLOR environment variables.
Setting the NO_COLOR variable to any non-empty value will disable color output. For example:
NO_COLOR=1 # Disable colors
NO_COLOR=true # Disable colorsRefer to the NO_COLOR standard for more details.
The FORCE_COLOR standard variable is used to control the color output in the terminal.
The behavior of FORCE_COLOR in Ansis follows the Node.js convention, with a few adaptations:
| Value | Description |
|---|---|
FORCE_COLOR=false |
Disables colors |
FORCE_COLOR=0 |
Disables colors |
FORCE_COLOR=true |
Auto-detects supported colors; enforces truecolor if none detected |
FORCE_COLOR=(unset) |
Auto-detects supported colors; enforces truecolor if none detected |
FORCE_COLOR=1 |
Enables 16 colors |
FORCE_COLOR=2 |
Enables 256 colors |
FORCE_COLOR=3 |
Enables truecolor |
Important
In Node.js FORCE_COLOR values of 1, true,
and an empty string ('') are treated as enabling 16 colors.
In Ansis:
1- enables exactly 16 colorstrue- and an empty string trigger automatic color detection (16, 256, or truecolor).
If no colors are detected,truecoloris enforced.
See:
For example, app.js:
import { red } from 'ansis';
console.log(red`red color`);You can test the following behaviors by executing the script in the terminal:
node app.js # Colored output in terminal
node app.js > log.txt # Output in file without ANSI codes
NO_COLOR=1 node app.js # Force disable colors
FORCE_COLOR=0 node app.js # Force disable colors
FORCE_COLOR=1 node app.js > log.txt # Force enable 16 colors
FORCE_COLOR=2 node app.js > log.txt # Force enable 256 colors
FORCE_COLOR=3 node app.js > log.txt # Force enable truecolorThe COLORTERM environment variable indicates color support in terminal emulators.
Its value depends on the terminal and its level of color support. Common values supported by Ansis are:
truecoloror24bit- 16 million colorsansi256- 256 colorsansi- 16 colors
To force a specific color level, you can set the COLORTERM variable before running the Node script:
COLORTERM=ansi node script.js # Force enable 16 colors
COLORTERM=ansi256 node script.js # Force enable 256 colors
COLORTERM=truecolor node script.js # Force enable truecolorAnsis automatically detects color support, but you can manually set the color level.
| Level | Description |
|---|---|
0 |
No colors (all colors disabled) |
1 |
Basic colors (16 colors) |
2 |
256 colors |
3 |
Truecolor (16 million colors) |
You can create a new instance of Ansis with the desired color level.
Disable colors:
import { Ansis } from 'ansis';
const custom = new Ansis(0);
console.log(custom.red`foo`); // Output: plain string, no ANSI codesUse only basic colors:
import { Ansis } from 'ansis';
const custom = new Ansis(1);
console.log(custom.hex('#FFAB40')`Orange`); // Output: fallback to yellowBrightExample
import { Ansis } from 'ansis';
/**
* Ansis instance for CLI that can be initialized with no colors mode
* needed for outputs where we don't want to have colors.
*
* @param {boolean} noColors Disable colors
* @return {Ansis} Default or custom instance
*/
const safeAnsis = function(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 automatically detects the supported color level (none, 16, 256, or truecolor) based on the environment.
To ensure consistent test results across different terminals and environments,
you can explicitly set the desired color level using one of the supported environment variables:
NO_COLOR, FORCE_COLOR or COLORTERM.
Important
You must define the environment variable before importing ansis.
process.env.NO_COLOR = '1'; // ❌ Doesn't work
import { red } from 'ansis'; // <- Too late! NO_COLOR is undefined when ansis loadedInstead, create a separate file to set the environment variable and import it first:
import './no-color.js'; // ✅ Sets env variable early
import { red } from 'ansis'; // NO_COLOR is definedTo ensure consistent test output without ANSI codes, you can disable color rendering using the NO_COLOR environment variable.
Create a file: no-color.js:
process.env.NO_COLOR = '1';Import this file first in your test:
import './no-color.js'; // disables colors
import { expect, test } from 'vitest';
import { red } from 'ansis';
console.log(red('foo')); // Output: plain "foo", no ANSI codes
test('output should not contain ANSI codes', () => {
const output = red('foo');
expect(output).toBe('foo');
});Alternatively, use ansis.strip() to remove color codes from strings in your tests:
import { expect, describe, test } from 'vitest';
import ansis, { red } from 'ansis';
test('should remove ANSI codes from output', () => {
const output = red('foo');
expect(ansis.strip(output)).toBe('foo');
});File: enable-truecolor.js:
process.env.COLORTERM = 'truecolor';Test file:
import './enable-truecolor.js'; // enables truecolor
import { red, fg, hex } from 'ansis';
console.log(hex('#FFAB40')('orange')); // uses native ANSI RGB
console.log(fg(200)('pink')); // uses ANSI 256
console.log(red('red')); // uses ANSI 16File: enable-256colors.js:
process.env.COLORTERM = 'ansi256';Test file:
import './enable-256colors.js'; // enables 256 colors
import { red, fg, hex } from 'ansis';
console.log(hex('#FFAB40')('orange')); // fallback to ANSI 256 colors
console.log(fg(200)('pink')); // uses ANSI 256 colors
console.log(red('red')); // uses ANSI 16 colorsFile: enable-16colors.js:
process.env.COLORTERM = 'ansi';Test file:
import './enable-16colors.js'; // enables 16 colors
import { red, fg, hex } from 'ansis';
console.log(hex('#FFAB40')('orange')); // fallback to ANSI 16 colors (e.g., bright red)
console.log(fg(200)('pink')); // fallback to ANSI 16 colors (e.g., bright magenta)
console.log(red('red')); // uses ANSI 16 colorsUse cmd arguments --no-color to disable colors and --color to enable ones.
For example, an executable script app.js:
#!/usr/bin/env node
import { red } from 'ansis';
console.log(red`text`);Execute the script in a terminal:
./app.js # colored output in terminal
./app.js --no-color # non colored output in terminal
./app.js > log.txt # output in file without ANSI codes
./app.js --color > log.txt # output in file with ANSI codes
Note
Command-line arguments take precedence over environment variables.
Ansis automatically detects the supported color level:
0– No color (black & white)1– Basic ANSI (16 colors)2– Extended ANSI (256 colors)3– Truecolor (24-bit RGB)
You can access the detected color level via the readonly level property:
import ansis from 'ansis';
console.log('Detected color level: ', ansis.level);To check if ANSI color output is supported, use the isSupported() method:
import ansis from 'ansis';
console.log('Color output supported:', ansis.isSupported());Note
There is no standard way to detect terminal color support.
The most common method is to check the TERM and COLORTERM environment variables, which often indicate the supported color level.
Most standard CI systems can be identified by the presence of the CI environment variable.
While some CI uses their own specific environment variables, they are inconsistent and not widely adopted.
Ansis provides basic support for standard CI environments by checking the commonly used CI environment variable.
In such cases, Ansis assumes support for at least 16 colors.
If your code uses 256-color or truecolor, Ansis automatically fallback to 16 colors, or to black and white if no color support is detected.
Ansis explicitly detects
GitHub Actionsas supportingtruecolor, as most Ansis users rely on GitHub CI.
Combined with information about the operating system, this approach provides a practical and lightweight method for detecting color support in most environments.
| Terminal | ANSI 16 colors |
ANSI 256 colors |
True Color |
env. TERM |
env. COLORTERM |
Specifically ENV variables |
|---|---|---|---|---|---|---|
| Azure CI | ✅ | ❌ | ❌ | dumb | TF_BUILD AGENT_NAME |
|
| GitHub CI | ✅ | ✅ | ✅ | dumb | CI, GITHUB_ACTIONS | |
| GitTea CI | ✅ | ✅ | ✅ | dumb | CI, GITEA_ACTIONS | |
| GitLab CI | ✅ | ❌ | ❌ | dumb | CI, GITLAB_CI | |
| Travis CI | ✅ | ❌ | ❌ | dumb | TRAVIS | |
| PM2 not isTTY |
✅1 | ✅1 | ✅1 | dumb | PM2_HOME pm_id |
|
| 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 |
See also:
Run the command to see the support of some features by various libraries:
npm run compare
| Library | Colors support | Features | ||||
|---|---|---|---|---|---|---|
- ESM | CJS- named import - naming colors |
16 | 256 | 16m | 🌐 | Fallback | Chained syntax |
Nested template strings `${}` |
LF\n |
Supports ENV vars CLI flags |
ansisESM CJS✅ named import✅ standard
|
✅ ✅ ✅ ✅ | →256 →16 →b&w |
✅ | ✅ | ✅ | NO_COLORFORCE_COLORCOLORTERM--no-color--color |
chalk v5ESM❌ named import✅ standard |
✅ ✅ ✅ ✅ | →256 →16 →b&w |
✅ | ❌ | ✅ | NO_COLORFORCE_COLOR--no-color--color |
koloristESM CJS✅ named import❌ standard |
✅ ✅ ✅ ❌ | →256 →b&w |
❌ | ❌ | ❌ | NO_COLORFORCE_COLOR |
cli-colorCJS❌ named import✅ standard |
✅ ✅ ❌ 🛑 | →16 →b&w |
✅ | ❌ | ❌ | NO_COLOR |
colors-cliCJS❌ named import❌ standard |
✅ ✅ ❌ 🛑 | →b&w | ✅ | ❌ | ✅ | --no-color--color |
colors.jsCJS❌ named import❌ standard |
✅ ❌ ❌ 🛑 | →b&w | ✅ | ❌ | ✅ | FORCE_COLOR--no-color--color |
ansi-colorsCJS❌ named import✅ standard |
✅ ❌ ❌ ❌ | ❌ | ✅ | ❌ | ✅ | FORCE_COLOR |
coloretteESM CJS✅ named import✅ standard |
✅ ❌ ❌ 🛑 | →b&w | ❌ | ❌ | ❌ | NO_COLORFORCE_COLOR--no-color--color |
picocolorsCJS❌ named import✅ standard |
✅ ❌ ❌ ❌ | →b&w | ❌ | ❌ | ❌ | NO_COLORFORCE_COLOR--no-color--color |
tinyrainbowESM❌ named import✅ standard |
✅ ❌ ❌ ✅ | →b&w | ❌ | ❌ | ❌ | NO_COLORFORCE_COLORFORCE_TTY--no-color--color |
kleurESM CJS✅ named import✅ standard |
❌ ❌ ❌ ✅8 colors |
→b&w | ✅ | ❌ | ❌ | NO_COLORFORCE_COLOR |
util.styleText()Node ≥ 22❌ named import✅ standard |
✅ ❌ ❌ 🛑 | →b&w | ❌ | ❌ | ? | NO_COLORFORCE_COLOR |
Named import
ESM
import { red, green, blue } from 'lib';
CJS
const { red, green, blue } = require('lib');
Naming colors
- standard: colors have standard names, e.g.:
red,redBright,bgRed,bgRedBright - non-standard: colors have lib-specific names, e.g.:
brightRed,bgBrightRed,red_b,red_btt
-
16- ANSI 16 colors likered,redBright,bgRed,bgRedBright -
256- ANSI 256 colors methods, e.g.:ansis:fg(n),bg(n)chalk:ansi256(n),bgAnsi256(n)cli-color:xterm(n)colors-cli:x<n>
-
16m- Truecolor methods, e.g.:hex(),bgHex(),rgb(),bgRgb() -
🌐 - Colored output in Chromium-based browser console:
- ✅ - colored output
- ❌ - b&w output
- 🛑 - fatal error by compilation or in runtime
-
Fallback - Truecolor → 256 colors → 16 colors → no colors
-
Chained syntax
lib.red.bold('text') -
Nested template strings
lib.red`text ${lib.cyan`nested`} text` -
LF- Correct break styles atend-of-line(\n).console.log(bgGreen('\nAnsis\nNew Line\nNext New Line\n'))
Compare how different libraries handle various input arguments in their functions.
| 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 redAnsis ensures consistent and predictable behavior for edge-case inputs, making it a reliable choice for usage.
| Package | Dependencies | Minified | Unpacked Size | Tarball size |
|---|---|---|---|---|
ansis |
0 | uglified & minified | 5.7 kB | 3.4 kB |
picocolors |
0 | no | 6.37 kB | 2.6 kB |
tinyrainbow |
0 | uglified | 8.1 kB | 3.2 kB |
colorette |
0 | no | 17.0 kB | 4.9 kB |
kleur |
0 | no | 20.3 kB | 6.0 kB |
ansi-colors |
0 | no | 26.1 kB | 8.5 kB |
kolorist |
0 | no | 51.0 kB | 8.7 kB |
colors.js |
0 | no | 41.5 kB | 11.1 kB |
chalk |
0 | no | 44.2 kB | 13.4 kB |
cli-color |
5 |
no | 754.0 kB | 216.8 kB |
colors-cli |
0 | no | 511.0 kB | 361.7 kB |
Legend
- Dependencies: Number of dependencies in the package.
- Is Minified: Indicates whether the distributed npm package is minified.
- Unpacked Size: The size of the npm package in the
node_modules/directory, (incl. dependencies). - Tarball size: The size of the downloaded
*.tgzpackage file.
You can check the package size with the following command:just replace thecurl -s -o package.tgz $(npm view <package-name> dist.tarball) && echo "Tarball size: $(stat -f%z package.tgz | awk '{printf "%.2f", $1/1024}') kB"
<package-name>with your package name.
See also:
- npmjs - show install size of the published package, w/o dependencies
- packagephobia - show total install size, incl. dependencies
- npm download size - show download size
- pkg-size - find the true size of an npm package
- bundlephobia - useless, doesn't show real tarball size of the downloaded npm package
git clone https://github.com/webdiscus/ansis.git
cd ./ansis
npm i
npm run demoSince Node.js v22, a built-in util.styleText() function was introduced to colorize terminal output without external dependencies.
This is a great step forward, it helps to reduce dependencies and keeps apps lighter.
However, many projects still run on Node < 22, where styleText is not available.
If you want to start using this new syntax already today but keep backward compatibility,
you can use a small adapter powered by Ansis.
import ansis from 'ansis';
export const styleText = (format, text) =>
(Array.isArray(format)
? format.reduce((style, name) => style[name], ansis)
: ansis[format]
)(text);import { styleText } from './styleText.js';
console.log(styleText('red', 'Error!'));
console.log(styleText(['red', 'bold'], 'Error!'));This works identically to the native util.styleText() API, but under the hood uses Ansis,
ensuring full color and style support across actual Node versions.
When your codebase drops support for legacy Node versions, you can switch instantly:
- import { styleText } from './styleText.js';
+ import { styleText } from 'utils';See also Ansis vs util.styleText() for a deeper comparison and performance benchmarks.
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.
- 🔴 TS1479: The current file is a CommonJS module whose imports will produce require calls
- 🟡 ESLint: Caution:
ansisalso has a named export
If you're using TypeScript in CommonJS project with the following tsconfig.json settings:
{
"compilerOptions": {
"module": "Node16",
"moduleResolution": "Node16"
}
}Then TypeScript will treat .ts files as either ESM or CommonJS based on the file extension or the type field in package.json:
.mts- always ES module.cts- always CommonJS.ts- ESM only if"type": "module"is set inpackage.json
Using import or import type in a file that is treated as CommonJS causes the error:
TS1479: The current file is a CommonJS module whose imports will produce require calls.
Warning
When using "moduleResolution": "Node16" or "NodeNext", TypeScript enforces strict ESM rules.
If your project is in CommonJS mode ("type": "commonjs"),
it does not allow importing ansis using import ansis from 'ansis', even with esModuleInterop enabled.
- Use
.mtsfile extension. This forces the file to be treated as an ES module. - Set
"type": "module"in yourpackage.jsonto tread a.tsfile as an ES module:Then this works:{ "type": "module" }import ansis, { type AnsiColors, Ansis, red, greenBright, hex } from 'ansis';
- Use CommonJS
require()(no type imports)const ansis = require('ansis'); const { Ansis, red, greenBright, hex } = ansis;
Caution
You cannot use import type in CommonJS files under "moduleResolution": "Node16" or "NodeNext"
- Switch to
"moduleResolution": "node"(if possible)
With"moduleResolution": "node"you can useimportandimport typein CommonJS files without errors:Use this only if your project doesn't rely on the strict behavior of{ "compilerOptions": { "module": "Node16", "moduleResolution": "node", "esModuleInterop": true } }"Node16".
If you use a default import:
import ansis from 'ansis';
console.log(ansis.red('Error!'));ESLint may show this warning:
ESLint: Caution:
ansisalso has a named exportred. Check if you meant to writeimport {red} from 'ansis'instead. (import/no-named-as-default-member)
Note
This warning is shown because ansis is a dual package: it provides both a default export and named exports.
ESLint's import/no-named-as-default-member rule is triggered when you import the default export and use its named properties,
to help catch possible mistakes with import syntax in dual-export modules.
-
Use named import (preferred):
import { red } from 'ansis'; console.log(red('Error!'));
-
If you want to keep existing code unchanged, use a namespace import (alternative):
import * as ansis from 'ansis'; console.log(ansis.red('Error!'));
-
Disable the rule for a single line:
// eslint-disable-next-line import/no-named-as-default-member import ansis from 'ansis'; console.log(ansis.red('Error!'));
-
Disable the rule globally in your ESLint config (not recommended):
// .eslintrc.js module.exports = { // ... rules: { 'import/no-named-as-default-member': 'off' } }
Caution
The benchmark results are meaningless numbers intended purely to promote the library and increase its popularity. All libraries are more than fast enough. These results only to show the effectiveness of micro-optimizations in the code, which does not impact on real-world usage.
Of course Picocolors will be a little bit faster in a micro-benchmark since it has less code and doesn't handles edge cases.
Taken from the comment by the creator of Chalk.
To measure performance is used benchmark.js.
Warning
Results of vitest benchmark are incorrect.
The vitest benchmark generate unreal results.
For example, the results of the simple bench:
chalk.red('foo') - 7.000.000 ops/sec
ansis.red('foo') - 23.000.000 ops/sec (x3 faster is incorrect result)
The actual performance results of Chalk and Ansis in this test are very similar.
git clone https://github.com/webdiscus/ansis.git
cd ./ansis
npm i
npm run build
npm run benchMacBook Pro 16" M1 Max 64GB
macOS Sequoia 15.1
Node.js v22.11.0
TerminaliTerm2v3.5.0
Important
Each library uses the recommended fastest styling method to compare the absolute performance.
In real practice, no one would use the slowest method (such as nested calls) to style a string when the library provides a faster and a shorter chained method.
For example:
lib.red.bold.bgWhite(' ERROR ') // fast and short
lib.red(lib.bold(lib.bgWhite(' ERROR '))) // slower and verboseThe simple test uses only single style.
Picocolors, Colorette and Kleur do not support chained syntax or correct style break (wenn used `\n` in a string),
so they are the fastest in this simple use case. No function, no performance overhead.
ansis.red('foo')
chalk.red('foo')
picocolors.red('foo')
styleText('red', 'foo')
...+ [email protected] 109.212.939 ops/sec
[email protected] 108.044.800 ops/sec
[email protected] 87.800.739 ops/sec
-> [email protected] 60.606.043 ops/sec
- [email protected] 55.702.479 ops/sec
[email protected] 37.069.069 ops/sec
[email protected] 14.364.378 ops/sec
[email protected] 7.060.583 ops/sec
[email protected] 2.753.751 ops/sec
[email protected] 897.746 ops/sec
- styleText 579.832 ops/secUsing only 2 styles, picocolors is already a bit slower, because applying multiple colours at once via chained syntax is faster than nested calls.
ansis.red.bold('foo')
chalk.red.bold('foo')
picocolors.red(picocolors.bold('foo')) // chained syntax is not supported
styleText(['red', 'bold'], 'foo')
...+ [email protected] 60.468.181 ops/sec
- [email protected] 58.777.183 ops/sec
- [email protected] 47.789.020 ops/sec
[email protected] 33.387.988 ops/sec
[email protected] 13.420.047 ops/sec
[email protected] 5.972.681 ops/sec
[email protected] 4.086.412 ops/sec
[email protected] 3.018.244 ops/sec
[email protected] 1.817.039 ops/sec
[email protected] 695.601 ops/sec
- styleText 561.290 ops/secUsing 3 styles, picocolors is 2x slower than ansis.
ansis.red.bold.bgWhite('foo')
chalk.red.bold.bgWhite('foo')
picocolors.red(picocolors.bold(picocolors.bgWhite('foo'))) // chained syntax is not supported
styleText(['red', 'bold', 'bgWhite'], 'foo')
...+ [email protected] 59.463.640 ops/sec
- [email protected] 42.166.783 ops/sec
- [email protected] 32.434.017 ops/sec
[email protected] 13.008.117 ops/sec
[email protected] 5.608.244 ops/sec
[email protected] 5.268.630 ops/sec
[email protected] 2.145.517 ops/sec
[email protected] 1.686.728 ops/sec
[email protected] 1.453.611 ops/sec
[email protected] 590.467 ops/sec
- styleText 550.498 ops/secIn rare cases, when using 4 styles, picocolors becomes 3.4x slower than ansis.
ansis.red.bold.underline.bgWhite('foo')
chalk.red.bold.underline.bgWhite('foo')
picocolors.red(picocolors.bold(picocolors.underline(picocolors.bgWhite('foo')))) // chained syntax is not supported
styleText(['red', 'bold', 'underline', 'bgWhite'], 'foo')
...+ [email protected] 59.104.535 ops/sec
- [email protected] 36.147.547 ops/sec
- [email protected] 17.581.709 ops/sec
[email protected] 7.981.171 ops/sec
[email protected] 4.825.665 ops/sec
[email protected] 3.729.880 ops/sec
[email protected] 1.514.053 ops/sec
[email protected] 1.229.999 ops/sec
[email protected] 1.210.931 ops/sec
[email protected] 481.073 ops/sec
- styleText 502.883 ops/secansis.red(`red ${ansis.green(`green`)} red`)
chalk.red(`red ${chalk.green(`green`)} red`)
picocolors.red(`red ${picocolors.green(`green`)} red`)
styleText('red', `red ${styleText('green', 'green')} red`)
...+ [email protected] 15.146.177 ops/sec
[email protected] 13.722.200 ops/sec
[email protected] 7.448.662 ops/sec
- [email protected] 7.325.956 ops/sec
- [email protected] 6.872.557 ops/sec
[email protected] 6.433.848 ops/sec
[email protected] 3.921.601 ops/sec
[email protected] 2.815.078 ops/sec
[email protected] 1.093.307 ops/sec
[email protected] 304.693 ops/sec
- styleText 286.335 ops/secThe complex test with deeply nested single styles.
c.green(
`green ${c.cyan(
`cyan ${c.red(
`red ${c.yellow(
`yellow ${c.blue(
`blue ${c.magenta(`magenta ${c.underline(`underline ${c.italic(`italic`)} underline`)} magenta`)} blue`,
)} yellow`,
)} red`,
)} cyan`,
)} green`,
)+ [email protected] 1.110.056 ops/sec
- [email protected] 1.073.299 ops/sec
-> [email protected] 847.246 ops/sec
[email protected] 847.110 ops/sec
- [email protected] 573.942 ops/sec
[email protected] 471.285 ops/sec
[email protected] 439.588 ops/sec
[email protected] 382.862 ops/sec
[email protected] 213.351 ops/sec
[email protected] 41.097 ops/secThe benchmark used in colorette for single styles.
c.red(`${c.bold(`${c.cyan(`${c.yellow('yellow')}cyan`)}`)}red`)+ [email protected] 3.861.384 ops/sec
[email protected] 3.815.039 ops/sec
-> [email protected] 2.918.269 ops/sec
[email protected] 2.548.564 ops/sec
- [email protected] 2.502.850 ops/sec
[email protected] 2.229.023 ops/sec
[email protected] 1.426.279 ops/sec
[email protected] 1.123.139 ops/sec
[email protected] 481.708 ops/sec
[email protected] 114.570 ops/secThe picocolors benchmark, slightly modified.
Added a bit more complexity by applying two styles to the colored word instead of one.
let index = 1e8;
c.red('.') +
c.yellow('.') +
c.green('.') +
c.red.bold(' ERROR ') +
c.red('Add plugin ' + c.cyan.underline('name') + ' to use time limit with ' + c.cyan(++index));+ [email protected] 2.601.559 ops/sec
-> [email protected] 2.501.227 ops/sec
[email protected] 2.326.491 ops/sec
- [email protected] 2.129.106 ops/sec
[email protected] 1.780.496 ops/sec
[email protected] 1.685.703 ops/sec
[email protected] 838.542 ops/sec
[email protected] 533.362 ops/sec
[email protected] 287.558 ops/sec
[email protected] 97.463 ops/secNote
In this test, which is closer to practical use, each library uses the fastest styling method available.
So, chalk, ansis, ansi-colors, cli-color, colors-cli and colors uses chained method, e.g. c.red.bold(' ERROR ').
While picocolors, colorette and kolorist uses nested calls, e.g. c.red(c.bold(' ERROR ')), because doesn't support the chained syntax.
Ansis is a powerful, small, and fast replacement for many similar libraries.
Just replace your import ... from ... or require(...) to ansis.
- Migrating from
chalk - Migrating from
colorette - Migrating from
picocolors - Migrating from
ansi-colors - Migrating from
kleur - Migrating from
cli-color
Migrating from chalk
- import chalk from 'chalk';
+ import chalk from 'ansis';Ansis supports the Chalk syntax and is compatible* with styles and color names, so you don't need to modify the original code:
chalk.red.bold('Error!');
// colorize "Error: file not found!"
chalk.red(`Error: ${chalk.cyan.bold('file')} not found!`);
// truecolor
chalk.hex('#FFA500').bold('Bold orange color');
chalk.rgb(123, 45, 67).underline('Underlined reddish color');
chalk.bgHex('#E0115F')('Ruby');
chalk.bgHex('#96C')('Amethyst');Warning
If used ANSI 256 colors functions, replace them with Ansis equivalents:
- chalk.ansi256(196)('Error');
+ ansis.fg((196)('Error');
- chalk.bgAnsi256(21)('Info');
+ ansis.bg(21)('Info');Warning
Ansis doesn't not support the overline style, because it's not widely supported and no one uses it.
Check you code and replace the overline style with standard underline:
- chalk.red.overline('text');
+ ansis.red.underline('text');Warning
Ansis support the common standard gray color name, not grey (UK spelling).
- chalk.grey('text');
+ ansis.gray('text');
- chalk.bgGrey('text');
+ ansis.bgGray('text');Optionally, you can rewrite the same code to make it even shorter and cleaner:
import { red, cyan, fg, bg, hex, rgb, bgHex, bgRgb } from 'ansis';
red.bold('Error!'); // using parentheses
red.bold`Error!`; // using template string
// colorize "Error: file not found!"
red`Error: ${cyan.bold`file`} not found!`;
// ANSI 256 colors
fg(93)`Violet color`; // equivalent for chalk.ansi256()
bg(194)`Honeydew, more or less`; // equivalent for chalk.bgAnsi256()
// truecolor
hex('#FFA500').bold`Bold orange color`;
rgb(123, 45, 67).underline`Underlined reddish color`;
bgHex('#E0115F')`Ruby`;
bgHex('#96C')`Amethyst`;Migrating from Chalk v4
When used the keyword color model, e.g. chalk.keyword('orange')
extend ansis instance with named colors.
import ansis from 'ansis';
import colorNames from 'css-color-names'; // install color names package (~6 kB)
const color = ansis.extend(colorNames);
// alternatively define a custom subset with only the names you actually use:
//const color = ansis.extend({ orange: '#ffa500' });Now you can use named color on extended color instance:
- chalk.keyword('orange')('text');
+ color.orange('text');
- chalk.bgKeyword('orange')('text');
+ color.bgOrange('text');Migrating from colorette
- import { red, bold, underline } from 'colorette';
+ import { red, bold, underline } from 'ansis';Ansis is fully compatible with colorette styles and color names, so you don't need to modify the
original code:
red.bold('Error!');
bold(`I'm ${red(`da ba ${underline("dee")} da ba`)} daa`);Optionally, you can rewrite the same code to make it even shorter and cleaner:
red.bold`Error!`;
bold`I'm ${red`da ba ${underline`dee`} da ba`} daa`;Migrating from picocolors
- import pico from 'picocolors';
+ import pico from 'ansis';Ansis is fully compatible with picocolors styles and color names, so you don't need to modify the
original code:
pico.red(pico.bold('text'));
pico.red(pico.bold(variable));
// colorize "Error: file not found!"
pico.red('Error: ' + pico.cyan(pico.bold('file')) + ' not found!');Optionally, you can rewrite the same code to make it even shorter and cleaner:
import { red, cyan } from 'ansis';
red.bold`text`;
red.bold(variable);
// colorize "Error: file not found!"
red`Error: ${cyan.bold`file`} not found!`Migrating from ansi-colors
- const c = require('ansi-colors');
+ const c = require('ansis');Ansis is fully compatible with ansi-color styles and color names, so you don't need to modify the
original code:
c.red.bold('Error!');
// colorize "Error: file not found!"
c.red(`Error: ${c.cyan.bold('file')} not found!`);Optionally, you can rewrite the same code to make it even shorter and cleaner:
import { red, cyan } from 'ansis';
red.bold('Error!'); // using parentheses
red.bold`Error!`; // using template string
// colorize "Error: file not found!"
red`Error: ${cyan.bold`file`} not found!`;Migrating from kleur
- import { red, green, yellow, cyan } from 'kleur';
+ import { red, green, yellow, cyan } from 'ansis';Ansis is fully compatible with kleur styles and color names,
but Kleur v3.0 no longer uses Chalk-style syntax (magical getter):
green().bold().underline('this is a bold green underlined message');
yellow(`foo ${red().bold('red')} bar ${cyan('cyan')} baz`);If you uses chained methods then it requires a simple code modification.
Just replace (). with .:
green.bold.underline('this is a bold green underlined message');
yellow(`foo ${red.bold('red')} bar ${cyan('cyan')} baz`);Optionally, you can rewrite the same code to make it even shorter and cleaner:
yellow`foo ${red.bold`red`} bar ${cyan`cyan`} baz`;Migrating from cli-color
- const clc = require('cli-color');
+ const clc = require('ansis');Ansis is compatible* with cli-color styles and color names:
clc.red.bold('Error!');
// colorize "Error: file not found!"
clc.red(`Error: ${c.cyan.bold('file')} not found!`);Warning
Ansis doesn't not support the blink style, because it's not widely supported and no one uses it.
Check you code and remove the blink style:
- clc.red.blink('text');
+ clc.red('text');If you use ANSI 256 color functions, replace xterm with fg and bgXterm replace bg:
- clc.xterm(202).bgXterm(236)('Orange text on dark gray background');
+ clc.fg(202).bg(236)('Orange text on dark gray background');Optionally, you can rewrite the same code to make it even shorter and cleaner:
import { red, cyan, fg, bg } from 'ansis';
red.bold`Error!`;
// colorize "Error: file not found!"
red`Error: ${cyan.bold`file`} not found!`;
fg(202).bg(236)`Orange text on dark gray background`;npm run test will run the unit and integration tests.
npm run test:coverage will run the tests with coverage.