{"id":18114,"library":"ansi-sequence-parser","title":"ansi-sequence-parser","description":"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.","status":"active","version":"1.1.3","language":"javascript","source_language":"en","source_url":"https://github.com/blake-mealey/ansi-sequence-parser","tags":["javascript","ansi","sequences","parser","typescript"],"install":[{"cmd":"npm install ansi-sequence-parser","lang":"bash","label":"npm"},{"cmd":"yarn add ansi-sequence-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add ansi-sequence-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM only; package does not provide CJS exports.","wrong":"const parseAnsiSequences = require('ansi-sequence-parser').parseAnsiSequences","symbol":"parseAnsiSequences","correct":"import { parseAnsiSequences } from 'ansi-sequence-parser'"},{"note":"Named export, not default.","wrong":"import createAnsiSequenceParser from 'ansi-sequence-parser'","symbol":"createAnsiSequenceParser","correct":"import { createAnsiSequenceParser } from 'ansi-sequence-parser'"},{"note":"ColorPalette is the return type, not a constructor. createColorPalette is the function.","wrong":"import { ColorPalette } from 'ansi-sequence-parser'","symbol":"createColorPalette","correct":"import { createColorPalette } from 'ansi-sequence-parser'"},{"note":"Exported as a plain object, not a function.","wrong":null,"symbol":"defaultNamedColorsMap","correct":"import { defaultNamedColorsMap } from 'ansi-sequence-parser'"}],"quickstart":{"code":"import { parseAnsiSequences, createColorPalette } from 'ansi-sequence-parser';\n\nconst input = '\\x1b[31mHello\\x1b[0m \\x1b[1;34mWorld\\x1b[0m';\nconst tokens = parseAnsiSequences(input);\nconst palette = createColorPalette();\n\nfor (const token of tokens) {\n  const fg = token.foreground ? palette.value(token.foreground) : null;\n  const bg = token.background ? palette.value(token.background) : null;\n  console.log({ value: token.value, foreground: fg, background: bg, decorations: [...token.decorations] });\n}","lang":"typescript","description":"Demonstrates parsing ANSI sequences and converting colors to hex using the default color palette."},"warnings":[{"fix":"Upgrade to v1.1.1 or later.","message":"The parser uses infinite loop in v1.1.0 and earlier when parsing an unclosed sequence.","severity":"gotcha","affected_versions":"<=1.1.0"},{"fix":"Provide a full custom palette or extend the default with defaultNamedColorsMap.","message":"createColorPalette() without arguments will throw if you try to use custom color indices beyond the default 16-color table.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always ensure sequences are terminated with a letter (e.g., 'm') to avoid parsing issues.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Pre-process input to remove non-SGR sequences if needed, or use a more complete parser.","message":"The library only supports SGR (Select Graphic Rendition) sequences; other ANSI escape sequences (like cursor movement) are ignored.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run 'npm install ansi-sequence-parser' and ensure your project uses ESM (type: 'module' in package.json or .mjs extension).","cause":"Package not installed or running in an environment that doesn't support ESM.","error":"Error: Cannot find module 'ansi-sequence-parser'"},{"fix":"Use: import { parseAnsiSequences } from 'ansi-sequence-parser'","cause":"Incorrect import (maybe default import instead of named).","error":"TypeError: parseAnsiSequences is not a function"},{"fix":"Check token.foreground or token.background for null before calling palette.value().","cause":"Calling palette.value() on null/undefined color object.","error":"TypeError: Cannot read properties of undefined (reading 'value')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}