Anser - ANSI Sequence Parser

raw JSON →
2.3.5 verified Sat Apr 25 auth: no javascript

Anser is a low-level parser for ANSI escape codes, converting terminal text with color/style sequences into HTML elements, JSON, or plain text. Version 2.3.5 is the current stable release, with infrequent updates focused on bug fixes and edge cases. Key differentiators: supports true color (24-bit), 256-color palette, and SGR parameters; provides both HTML and JSON output; includes link detection and HTML escaping; has TypeScript definitions. Alternatives like chalk focus on terminal output, not parsing, while ansi-to-html is less feature-rich. Released under MIT license.

error Cannot find module 'anser' or its corresponding type declarations.
cause Missing @types/anser or package not installed.
fix
npm install anser (types included) or yarn add anser. No separate @types needed.
error TypeError: Anser.ansiToHtml is not a function
cause Using default import without default export syntax in CommonJS environment.
fix
Use import Anser from 'anser' (ESM) or const Anser = require('anser').default (CommonJS).
error Module '"anser"' has no exported member 'ansiToHtml'.
cause Trying named import from anser when it's not exported that way in TypeScript without esModuleInterop.
fix
Use import Anser from 'anser' and call Anser.ansiToHtml, or enable esModuleInterop.
error Cannot use import statement outside a module
cause Using ESM import syntax in a CommonJS script without 'type':'module' in package.json.
fix
Add "type": "module" to package.json or rename file to .mjs.
breaking ESM-only package since v2 with no CommonJS main export; require('anser') may not work in Node.js without package.json exports field support.
fix Use import syntax or set type: 'module' in package.json. For CommonJS, use dynamic import or await import('anser').
deprecated The ansiToJson method's output includes a 'isEmpty' function property which may be removed in future versions.
fix Access json object properties directly without relying on isEmpty function. Use content, fg, bg, etc.
gotcha TypeScript definition export changed in v2.0.2; importing as namespace may fail without esModuleInterop.
fix Enable esModuleInterop in tsconfig.json, or use import Anser = require('anser') and use as namespace.
gotcha ansiToHtml with use_classes uses palette-based class names (e.g., 'ansi-palette-196-fg') which may not match CSS frameworks expecting different naming.
fix Customize class names by mapping the palette yourself, or use inline styles (default).
deprecated The 'linkify' option in ansiToHtml was removed in v2.0.0; use external linkification.
fix Pre-process text with a linkify library before passing to anser.
gotcha Grayscale palette colors (in 256-color mode) may return undefined for some indices.
fix Update to v2.3.5+ which fixes grayscale palette returning undefined.
npm install anser
yarn add anser
pnpm add anser

Demonstrates converting ANSI-colored text to HTML with inline styles, CSS classes, JSON, and plain text removal.

import Anser from 'anser';

const txt = "\u001b[38;5;196mHello\u001b[39m \u001b[48;5;226mWorld\u001b[49m";

// Convert to HTML with inline styles
const html = Anser.ansiToHtml(txt);
console.log(html);
// <span style="color:rgb(255, 0, 0)">Hello</span> <span style="background-color:rgb(255, 255, 0)">World</span>

// Convert to HTML with CSS classes
const htmlClasses = Anser.ansiToHtml(txt, { use_classes: true });
console.log(htmlClasses);
// <span class="ansi-palette-196-fg">Hello</span> <span class="ansi-palette-226-bg">World</span>

// Convert to JSON
const json = Anser.ansiToJson(txt);
console.log(JSON.stringify(json[1]));
// {"content":"Hello","fg":"255, 0, 0","bg":null,"fg_truecolor":null,"bg_truecolor":null,"clearLine":false,"decoration":null,"was_processed":true}

// Remove ANSI codes (plain text)
const plain = Anser.ansiToText(txt);
console.log(plain);
// Hello World