← Back to archive

Repo of the Day

webdiscus/ansis

Published: Jul 9, 2024

Open repository ↗

Small and fast Node.js CJS/ESM lib to colorize terminal output. Lightweight alternative to Chalk with more features. Supports Bun, Deno, Next.JS

Summary

ansis is a small ANSI color library (5.8 kB) for JavaScript and TypeScript that works across Node.js, Bun, Deno, Next.js, and Chromium-based browser consoles. It exposes a chainable, template-literal-friendly API for 16, 256, and truecolor (hex/rgb) output, and automatically detects the best color level available in the current runtime, falling back from truecolor to 256 colors to 16 colors to no color.

What it is useful for

ansis is a practical choice when you need consistent, colored console output across heterogeneous environments. It is documented as a drop-in replacement for chalk, ansi-colors, and colorette, and the README lists support for ESM, CJS, TypeScript, Bun, Deno, Next.js (including edge runtime), GitHub Actions and other CI systems, and Chromium browsers such as Chrome, Edge, and Brave. It is useful for:

  • Building CLIs where you want truecolor output, e.g. hex('#FF75D1').bold`Pink` and rgb(224, 17, 95).italicRuby``.
  • Standardizing logs in CI, where non-TTY output still benefits from auto-detection of COLORTERM, CI, and platform-specific signals.
  • Stripping ANSI codes for tests or logs via ansis.strip(red('text')).
  • Creating terminal hyperlinks with blue.underline.link('https://example.com', 'Click here') (the README notes link() must be called last in the chain).
  • Registering custom named colors through new Ansis().extend({ orange: '#ffa500' }) to build a small theme.

How engineers can use it

The README documents a straightforward setup:

  1. Install via npm install ansis. For Node 10–12, use npm install ansis@node10.
  2. Import either a default object or named functions:
    import ansis, { red, cyan, bold, hex, rgb } from 'ansis';
    // or in CJS:
    // const { red, bold, hex } = require('ansis');
    
  3. Use the chainable template-literal API without parentheses:
    console.log(bold.bgRed` FAIL `);
    console.log(red`✖ Error: ${cyan`file.js`} not found`);
    
  4. Override detection when needed by instantiating directly: new Ansis(0) forces plain text, new Ansis(1) forces 16 colors, and new Ansis() auto-detects. The current level is exposed at ansis.level and via ansis.isSupported().
  5. Control output from the shell using documented conventions: NO_COLOR=1 disables colors, FORCE_COLOR=1|2|3 forces 16/256/truecolor, and --no-color / --color flags are recognized. Note that the FORCE_COLOR=0 semantics follow Node.js (disable) rather than the original force-color.org rule.

Documented limitations to keep in mind: extended named colors must be created on a new Ansis instance via extend() (not the default import), and the README notes that for trivial 16-color needs on Node 22+, the built-in util.styleText() is a lighter alternative. For benchmark details, troubleshooting, and migration notes from chalk, see the linked docs/ pages in the README.