{"id":18603,"library":"node-ansiparser","title":"node-ansiparser","description":"A parser for ANSI escape code sequences implementing the state machine described at vt100.net/emu/dec_ansi_parser. Version 2.2.1 is the latest stable release, with no further updates expected. It provides callbacks for printable characters, OSC, execute, CSI, ESC, and DCS sequences. Unlike alternatives (e.g., ansi-parser), this library follows the DEC ANSI parser specification closely, supports unicode, and allows injection of error handling. It is designed for Node.js and requires only a terminal object with specific callback methods.","status":"active","version":"2.2.1","language":"javascript","source_language":"en","source_url":"https://github.com/netzkolchose/node-ansiparser","tags":["javascript","ansi","parser","terminal","escape sequence"],"install":[{"cmd":"npm install node-ansiparser","lang":"bash","label":"npm"},{"cmd":"yarn add node-ansiparser","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-ansiparser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM import is the modern usage; CommonJS require still works but may trigger deprecation.","wrong":"const AnsiParser = require('node-ansiparser')","symbol":"default","correct":"import AnsiParser from 'node-ansiparser'"},{"note":"Named export is available; many users expect a default export.","wrong":"const { AnsiParser } = require('node-ansiparser')","symbol":"AnsiParser","correct":"import { AnsiParser } from 'node-ansiparser'"},{"note":"For TypeScript, use type-only import if only using types; the package ships no TS declarations.","wrong":"import AnsiParser from 'node-ansiparser'","symbol":"type","correct":"import type { AnsiParser } from 'node-ansiparser'"}],"quickstart":{"code":"import AnsiParser from 'node-ansiparser';\n\nconst terminal = {\n  inst_p: (s) => console.log('print', s),\n  inst_o: (s) => console.log('osc', s),\n  inst_x: (flag) => console.log('execute', flag.charCodeAt(0)),\n  inst_c: (collected, params, flag) => console.log('csi', collected, params, flag),\n  inst_e: (collected, flag) => console.log('esc', collected, flag),\n  inst_H: (collected, params, flag) => console.log('dcs_hook', collected, params, flag),\n  inst_P: (data) => console.log('dcs_put', data),\n  inst_U: () => console.log('dcs_unhook'),\n  inst_E: (e) => { console.warn('error', e); return { abort: false }; }\n};\n\nconst parser = new AnsiParser(terminal);\nparser.parse('\\x1b[31mHello\\x1b[0m\\x1b]0;title\\x07');\nparser.reset();","lang":"javascript","description":"Demonstrates basic ANSI escape sequence parsing with a terminal object that logs all callbacks."},"warnings":[{"fix":"Ensure unicode input is cleanly split; avoid sending partial multibyte characters across parse calls.","message":"Unicode high characters (>0x9f) are only allowed in GROUND, OSC_STRING, and DCS_PASSTHROUGH states; elsewhere they cancel the escape sequence.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Buffer partial characters manually; the parser does not reassemble multibyte sequences across calls.","message":"The inst_p callback may be called with an incomplete character if a multibyte character is split across parse calls.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use import AnsiParser from 'node-ansiparser' instead of require.","message":"Node.js require() usage may be deprecated in future; prefer ESM import.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Always implement all callbacks (inst_p, inst_o, inst_x, inst_c, inst_e, inst_H, inst_P, inst_U) to avoid unexpected behavior.","message":"If the terminal object does not implement all required methods, the parser injects dummy methods silently.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Return { abort: true } from inst_E to abort parsing on error.","message":"The inst_E error callback expects a specific return object to abort; returning nothing defaults to continue with GROUND state.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Add an inst_x method to your terminal object: inst_x(flag) { console.log('execute', flag.charCodeAt(0)); }","cause":"Terminal object is missing the inst_x method.","error":"TypeError: inst_x is not a function"},{"fix":"Use import statement: import AnsiParser from 'node-ansiparser'","cause":"Using CommonJS require in a project with ESM-only settings.","error":"SyntaxError: Unexpected token '?'"},{"fix":"Ensure you have installed the package and use correct import: 'node-ansiparser'.","cause":"Import path or module resolution issue.","error":"ReferenceError: AnsiParser is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}