messageformat-parser
raw JSON → 4.1.3 verified Sat Apr 25 auth: no javascript
A PEG.js-based parser for ICU MessageFormat strings, used by the messageformat library to parse ICU message patterns into an AST. Current stable version is 4.1.3, with a release cadence aligned with the messageformat monorepo. Key differentiators: implements the ICUMessageFormat specification, supports plural, select, and complex patterns, and is the heritage parser before the shift to MF2 in messageformat v4. It is lightweight and focused solely on parsing.
Common errors
error Cannot find module 'messageformat-parser' ↓
cause Package not installed or version mismatch.
fix
Run 'npm install messageformat-parser' and ensure it's in your dependencies.
error SyntaxError: Expected "{" ... ↓
cause Invalid ICU message syntax, e.g., unescaped quotes or mismatched braces.
fix
Check message format specification and escape single quotes with ''.
error TypeError: parse is not a function ↓
cause Importing default instead of named export.
fix
Use import { parse } from 'messageformat-parser' instead of importing default.
Warnings
breaking messageformat v4 switched to a new MF2 parser; this package is for legacy ICUMessageFormat only. ↓
fix Use messageformat (the full package) for MF2 support, or continue using messageformat-parser for ICU MessageFormat.
gotcha The parser does not implement string interpolation or variable substitution; it only returns an AST. ↓
fix Use messageformat or another library to format the AST into a string.
deprecated parse() now returns a simpler AST in v4; check types for migration from v3. ↓
fix Update your code to use the new AST shape: 'type' property is string, not a number.
Install
npm install messageformat-parser yarn add messageformat-parser pnpm add messageformat-parser Imports
- parse wrong
import parse from 'messageformat-parser'correctimport { parse } from 'messageformat-parser' - PegParser
import { PegParser } from 'messageformat-parser'
Quickstart
import { parse } from 'messageformat-parser';
const ast = parse('You have {count, plural, one {# item} other {# items}}.');
console.log(JSON.stringify(ast, null, 2));
// Output shows parsed AST with type, plural, etc.