Registry / serialization / ansi-sequence-parser

ansi-sequence-parser

JSON →
library1.1.3jsnpmunverified

A parser for ANSI escape sequences that tokenizes input into structured objects with foreground/background colors and decorations. Current stable version is 1.1.3. Released with minor features and bug fixes since its initial v1.0.0 in 2023. Key differentiator: it supports incremental parsing via a stateful parser, provides a color palette to interpret named, table, and RGB colors as hex codes, and ships TypeScript types. Suitable for generating pretty HTML or custom rendering from terminal output.

npm install ansi-sequence-parser
INSTALL
IMPORT
SIG · ANSI-SEQUENCE-PARS
A
ansi-sequence-parser
serializationjavascriptv1.1.3
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.

parseAnsiSequences
import { parseAnsiSequences } from 'ansi-sequence-parser'
const parseAnsiSequences = require('ansi-sequence-parser').parseAnsiSequences
ESM only; package does not provide CJS exports.
createAnsiSequenceParser
import { createAnsiSequenceParser } from 'ansi-sequence-parser'
import createAnsiSequenceParser from 'ansi-sequence-parser'
Named export, not default.
createColorPalette
import { createColorPalette } from 'ansi-sequence-parser'
import { ColorPalette } from 'ansi-sequence-parser'
ColorPalette is the return type, not a constructor. createColorPalette is the function.
defaultNamedColorsMap
import { defaultNamedColorsMap } from 'ansi-sequence-parser'
Exported as a plain object, not a function.

Demonstrates parsing ANSI sequences and converting colors to hex using the default color palette.

import { parseAnsiSequences, createColorPalette } from 'ansi-sequence-parser'; const input = '\x1b[31mHello\x1b[0m \x1b[1;34mWorld\x1b[0m'; const tokens = parseAnsiSequences(input); const palette = createColorPalette(); for (const token of tokens) { const fg = token.foreground ? palette.value(token.foreground) : null; const bg = token.background ? palette.value(token.background) : null; console.log({ value: token.value, foreground: fg, background: bg, decorations: [...token.decorations] }); }
Debug
Known issues
gotchaThe parser uses infinite loop in v1.1.0 and earlier when parsing an unclosed sequence.
fix
Upgrade to v1.1.1 or later.
affects: <=1.1.0
gotchacreateColorPalette() without arguments will throw if you try to use custom color indices beyond the default 16-color table.
fix
Provide a full custom palette or extend the default with defaultNamedColorsMap.
affects: >=1.0.0
gotchaThe parser does not handle all edge cases; e.g., SGR sequences with multiple parameters separated by semicolons may produce unexpected results if not properly terminated.
fix
Always ensure sequences are terminated with a letter (e.g., 'm') to avoid parsing issues.
affects: >=1.0.0
gotchaThe library only supports SGR (Select Graphic Rendition) sequences; other ANSI escape sequences (like cursor movement) are ignored.
fix
Pre-process input to remove non-SGR sequences if needed, or use a more complete parser.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'ansi-sequence-parser'
Package not installed or running in an environment that doesn't support ESM.
fix
Run 'npm install ansi-sequence-parser' and ensure your project uses ESM (type: 'module' in package.json or .mjs extension).
TypeError: parseAnsiSequences is not a function
Incorrect import (maybe default import instead of named).
fix
Use: import { parseAnsiSequences } from 'ansi-sequence-parser'
TypeError: Cannot read properties of undefined (reading 'value')
Calling palette.value() on null/undefined color object.
fix
Check token.foreground or token.background for null before calling palette.value().
Upgrade
Version history
1.1.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
16
OpenAI (training)
1
Resources
ansi-sequence-parser — npm install ansi-sequence-parser · libregistry