{"id":18111,"library":"anser","title":"Anser - ANSI Sequence Parser","description":"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.","status":"active","version":"2.3.5","language":"javascript","source_language":"en","source_url":"git://github.com/IonicaBizau/anser","tags":["javascript","ansi","html","typescript"],"install":[{"cmd":"npm install anser","lang":"bash","label":"npm"},{"cmd":"yarn add anser","lang":"bash","label":"yarn"},{"cmd":"pnpm add anser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM default import. For CommonJS use require('anser').default or use the import syntax shown. TypeScript with --esModuleInterop uses default import; without, use import Anser = require('anser').","wrong":"const Anser = require('anser')","symbol":"Anser","correct":"import Anser from 'anser'"},{"note":"Named export for the function. Alternatively use Anser.ansiToHtml as a static method.","wrong":"import { Anser } from 'anser'","symbol":"ansiToHtml","correct":"import { ansiToHtml } from 'anser'"},{"note":"Helper function to escape HTML entities. Available as named export.","wrong":"const escapeForHtml = require('anser').escapeForHtml","symbol":"escapeForHtml","correct":"import { escapeForHtml } from 'anser'"}],"quickstart":{"code":"import Anser from 'anser';\n\nconst txt = \"\\u001b[38;5;196mHello\\u001b[39m \\u001b[48;5;226mWorld\\u001b[49m\";\n\n// Convert to HTML with inline styles\nconst html = Anser.ansiToHtml(txt);\nconsole.log(html);\n// <span style=\"color:rgb(255, 0, 0)\">Hello</span> <span style=\"background-color:rgb(255, 255, 0)\">World</span>\n\n// Convert to HTML with CSS classes\nconst htmlClasses = Anser.ansiToHtml(txt, { use_classes: true });\nconsole.log(htmlClasses);\n// <span class=\"ansi-palette-196-fg\">Hello</span> <span class=\"ansi-palette-226-bg\">World</span>\n\n// Convert to JSON\nconst json = Anser.ansiToJson(txt);\nconsole.log(JSON.stringify(json[1]));\n// {\"content\":\"Hello\",\"fg\":\"255, 0, 0\",\"bg\":null,\"fg_truecolor\":null,\"bg_truecolor\":null,\"clearLine\":false,\"decoration\":null,\"was_processed\":true}\n\n// Remove ANSI codes (plain text)\nconst plain = Anser.ansiToText(txt);\nconsole.log(plain);\n// Hello World","lang":"typescript","description":"Demonstrates converting ANSI-colored text to HTML with inline styles, CSS classes, JSON, and plain text removal."},"warnings":[{"fix":"Use import syntax or set type: 'module' in package.json. For CommonJS, use dynamic import or await import('anser').","message":"ESM-only package since v2 with no CommonJS main export; require('anser') may not work in Node.js without package.json exports field support.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Access json object properties directly without relying on isEmpty function. Use content, fg, bg, etc.","message":"The ansiToJson method's output includes a 'isEmpty' function property which may be removed in future versions.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Enable esModuleInterop in tsconfig.json, or use import Anser = require('anser') and use as namespace.","message":"TypeScript definition export changed in v2.0.2; importing as namespace may fail without esModuleInterop.","severity":"gotcha","affected_versions":">=2.0.2"},{"fix":"Customize class names by mapping the palette yourself, or use inline styles (default).","message":"ansiToHtml with use_classes uses palette-based class names (e.g., 'ansi-palette-196-fg') which may not match CSS frameworks expecting different naming.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Pre-process text with a linkify library before passing to anser.","message":"The 'linkify' option in ansiToHtml was removed in v2.0.0; use external linkification.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Update to v2.3.5+ which fixes grayscale palette returning undefined.","message":"Grayscale palette colors (in 256-color mode) may return undefined for some indices.","severity":"gotcha","affected_versions":"<=2.3.4"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"npm install anser (types included) or yarn add anser. No separate @types needed.","cause":"Missing @types/anser or package not installed.","error":"Cannot find module 'anser' or its corresponding type declarations."},{"fix":"Use import Anser from 'anser' (ESM) or const Anser = require('anser').default (CommonJS).","cause":"Using default import without default export syntax in CommonJS environment.","error":"TypeError: Anser.ansiToHtml is not a function"},{"fix":"Use import Anser from 'anser' and call Anser.ansiToHtml, or enable esModuleInterop.","cause":"Trying named import from anser when it's not exported that way in TypeScript without esModuleInterop.","error":"Module '\"anser\"' has no exported member 'ansiToHtml'."},{"fix":"Add \"type\": \"module\" to package.json or rename file to .mjs.","cause":"Using ESM import syntax in a CommonJS script without 'type':'module' in package.json.","error":"Cannot use import statement outside a module"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}