Flow JavaScript Parser

0.309.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to parse a Flow-typed JavaScript code snippet using the `flow-parser` in Node.js, enabling Flow types and specific features like enums, and logging the root AST type and specific node names.

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);
}

view raw JSON →