{"id":10905,"library":"flow-parser","title":"Flow JavaScript Parser","description":"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.","status":"active","version":"0.309.0","language":"javascript","source_language":"en","source_url":"https://github.com/facebook/flow","tags":["javascript"],"install":[{"cmd":"npm install flow-parser","lang":"bash","label":"npm"},{"cmd":"yarn add flow-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add flow-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary API is accessed via a property on the default CommonJS export. Direct named ESM imports are not typically supported for the top-level `parse` function.","wrong":"import { parse } from 'flow-parser';","symbol":"parse","correct":"const flowParser = require('flow-parser');\nflowParser.parse('code', {});"},{"note":"When used in a browser environment via a `<script>` tag, the parser exposes a global `flow` object. Ensure `flow_parser.js` is loaded first.","symbol":"flow","correct":"<script src=\"flow_parser.js\"></script>\n<script>\n  flow.parse('code', {});\n</script>"}],"quickstart":{"code":"const flowParser = require('flow-parser');\n\nconst code = `\n  // @flow\n  enum Status { Active, Inactive };\n  function greet(name: string): string {\n    return 'Hello, ' + name + '!';\n  }\n  const status: Status = Status.Active;\n`;\n\nconst options = {\n  types: true, // Enable parsing of Flow types\n  comments: true, // Attach comments to AST nodes\n  enums: true, // Enable parsing of enums\n  tokens: false\n};\n\ntry {\n  const ast = flowParser.parse(code, options);\n  console.log('Successfully parsed code. AST root type:', ast.type);\n  console.log('Function declaration:', ast.body.find(node => node.type === 'FunctionDeclaration').id.name);\n  console.log('Enum declaration:', ast.body.find(node => node.type === 'EnumDeclaration').id.name);\n} catch (e) {\n  console.error('Parsing error:', e.message);\n}\n","lang":"javascript","description":"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."},"warnings":[{"fix":"Update AST traversal logic and type checks to accommodate the new `EnumBody` structure and its `members` property for accessing individual enum members.","message":"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.","severity":"breaking","affected_versions":">=0.308.0"},{"fix":"Ensure that the code being parsed adheres strictly to Flow's grammar for optional modifiers, removing any incorrect usages.","message":"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`.","severity":"breaking","affected_versions":">=0.307.0"},{"fix":"Always provide an options object to `flowParser.parse()` and set the relevant boolean flags (e.g., `enums: true`, `components: true`) to `true` for features present in the source code.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Access the `parse` method as a property of the imported object: `const flowParser = require('flow-parser'); flowParser.parse('code', {});`","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.","error":"TypeError: flowParser.parse is not a function"},{"fix":"Pass the appropriate option to `flowParser.parse()`, for example, `flowParser.parse(code, { enums: true, components: true });`","cause":"Using Flow-specific syntax (e.g., enums, components, decorators) without enabling the corresponding parsing option.","error":"SyntaxError: Unexpected token (X:Y)"}],"ecosystem":"npm"}