Flow JavaScript Parser
The flow-parser package provides a JavaScript parser, originally written in OCaml and compiled to JavaScript. It is designed to produce an Abstract Syntax Tree (AST) that largely conforms to the ESTree specification, similar to parsers like Esprima. This package is specifically built to understand and parse Flow's type annotations, making it a crucial component for tools that process Flow-typed code. As of April 2026, the current stable version is `0.309.0`, with releases appearing to follow a regular, often monthly or bi-monthly, cadence. Its key differentiator lies in its deep integration with the Flow type system and its ability to parse advanced Flow-specific syntax, while still outputting a widely compatible ESTree AST. It runs in both Node.js environments (requiring Node.js >= 0.4.0) and web browsers through a script tag.
Common errors
-
TypeError: flowParser.parse is not a function
cause Attempting to call `flowParser.parse()` after `require('flow-parser')` when `flow-parser` does not export a direct `parse` function, but rather an object that contains the `parse` method.fixAccess the `parse` method as a property of the imported object: `const flowParser = require('flow-parser'); flowParser.parse('code', {});` -
SyntaxError: Unexpected token (X:Y)
cause Using Flow-specific syntax (e.g., enums, components, decorators) without enabling the corresponding parsing option.fixPass the appropriate option to `flowParser.parse()`, for example, `flowParser.parse(code, { enums: true, components: true });`
Warnings
- breaking The AST structure for Enum body nodes has been significantly refactored. The five specific enum body ESTree node types (`EnumBooleanBody`, `EnumNumberBody`, etc.) have been replaced with a single `EnumBody` node. Its `members` property is now a flat array of per-type member nodes, and `explicitType` is a string or `null` instead of a boolean.
- breaking Parsing now errors when an optional modifier `?` appears in invalid contexts, such as `const x? = 1;`. Code that previously parsed but was syntactically incorrect in Flow will now throw a `ParseError`.
- gotcha Many Flow-specific language features, such as `enums`, `match` expressions, `components`, and `esproposal_decorators`, require explicit enabling via options passed to the `parse` method. By default, these advanced features are not parsed.
Install
-
npm install flow-parser -
yarn add flow-parser -
pnpm add flow-parser
Imports
- parse
import { parse } from 'flow-parser';const flowParser = require('flow-parser'); flowParser.parse('code', {}); - flow
<script src="flow_parser.js"></script> <script> flow.parse('code', {}); </script>
Quickstart
const flowParser = require('flow-parser');
const code = `
// @flow
enum Status { Active, Inactive };
function greet(name: string): string {
return 'Hello, ' + name + '!';
}
const status: Status = Status.Active;
`;
const options = {
types: true, // Enable parsing of Flow types
comments: true, // Attach comments to AST nodes
enums: true, // Enable parsing of enums
tokens: false
};
try {
const ast = flowParser.parse(code, options);
console.log('Successfully parsed code. AST root type:', ast.type);
console.log('Function declaration:', ast.body.find(node => node.type === 'FunctionDeclaration').id.name);
console.log('Enum declaration:', ast.body.find(node => node.type === 'EnumDeclaration').id.name);
} catch (e) {
console.error('Parsing error:', e.message);
}