Registry / serialization / anser
library2.3.5jsnpmunverified

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.

npm install anser
INSTALL
IMPORT
SIG · ANSER
A
anser
serializationjavascriptv2.3.5
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Anser
import Anser from 'anser'
const Anser = require('anser')
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').
ansiToHtml
import { ansiToHtml } from 'anser'
import { Anser } from 'anser'
Named export for the function. Alternatively use Anser.ansiToHtml as a static method.
escapeForHtml
import { escapeForHtml } from 'anser'
const escapeForHtml = require('anser').escapeForHtml
Helper function to escape HTML entities. Available as named export.

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
Debug
Known issues
breakingESM-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').
affects: >=2.0.0
deprecatedThe 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.
affects: >=2.0.0
gotchaTypeScript 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.
affects: >=2.0.2
gotchaansiToHtml 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).
affects: >=1.0.0
deprecatedThe '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.
affects: >=2.0.0
gotchaGrayscale palette colors (in 256-color mode) may return undefined for some indices.
fix
Update to v2.3.5+ which fixes grayscale palette returning undefined.
affects: <=2.3.4
Errors
Common errors & fixes
Cannot find module 'anser' or its corresponding type declarations.
Missing @types/anser or package not installed.
fix
npm install anser (types included) or yarn add anser. No separate @types needed.
TypeError: Anser.ansiToHtml is not a function
Using default import without default export syntax in CommonJS environment.
fix
Use import Anser from 'anser' (ESM) or const Anser = require('anser').default (CommonJS).
Module '"anser"' has no exported member 'ansiToHtml'.
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.
Cannot use import statement outside a module
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.
Upgrade
Version history
2.3.5latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
32 hits · last 30 days
node
26
OpenAI (training)
1
Resources
anser — npm install anser · libregistry