{"id":11031,"library":"hermes-parser","title":"Hermes Parser","description":"Hermes Parser is a robust JavaScript parser built directly from the Hermes engine's parser, compiled to WebAssembly. This allows for high-performance parsing of JavaScript code in various environments, including Node.js and browsers. As of version 0.35.0, it supports parsing ES6+ syntax, Flow, and JSX. A key differentiator is its ability to output Abstract Syntax Trees (ASTs) in either the standard ESTree format or a Babel-compatible format, offering flexibility for integration into different tooling ecosystems. It also provides fine-grained control over Flow parsing, allowing users to explicitly enable Flow syntax detection or parse all ambiguous syntax as Flow. The project is actively maintained by Facebook, ensuring ongoing development and compatibility with modern JavaScript features.","status":"active","version":"0.35.0","language":"javascript","source_language":"en","source_url":"git@github.com:facebook/hermes","tags":["javascript"],"install":[{"cmd":"npm install hermes-parser","lang":"bash","label":"npm"},{"cmd":"yarn add hermes-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add hermes-parser","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required if `transformOptions.TransformEnumSyntax` is enabled and no custom `getRuntime` is provided.","package":"flow-enums-runtime","optional":true}],"imports":[{"note":"The library primarily uses named exports. While CommonJS `require` might work in some environments, ESM `import` is the idiomatic and recommended approach.","wrong":"const parse = require('hermes-parser');","symbol":"parse","correct":"import { parse } from 'hermes-parser';"},{"note":"For TypeScript users, import `ParseOptions` as a type for robust type checking of the `parse` function's options argument.","symbol":"ParseOptions","correct":"import type { ParseOptions } from 'hermes-parser';"},{"note":"For TypeScript users, import `Node` or other AST types (like `Program`, `ExpressionStatement`) to correctly type the AST output depending on the chosen `babel` option.","symbol":"Node","correct":"import type { Node } from 'hermes-parser';"}],"quickstart":{"code":"import { parse } from 'hermes-parser';\n\nconst codeWithFlowAndJSX = `\n// @flow\ntype User = {\n  id: string,\n  name: string,\n  isAdmin?: boolean,\n};\n\nconst user: User = { id: '1', name: 'Alice' };\n\nfunction greet(person: User): React.Element {\n  return <div>Hello, {person.name}!</div>;\n}\n\nconst element = greet(user);\n`;\n\n// Parse with ESTree format (default) and detect Flow via pragma\nconst estreeAst = parse(codeWithFlowAndJSX, {\n  sourceType: 'module',\n  flow: 'detect',\n  sourceFilename: 'flow-jsx-example.js',\n});\n\nconsole.log('--- ESTree AST (excerpt) ---');\nconsole.log(JSON.stringify(estreeAst.body[0], null, 2));\n\n// Parse with Babel format and force all Flow parsing\nconst babelAst = parse(codeWithFlowAndJSX, {\n  babel: true,\n  sourceType: 'module',\n  flow: 'all',\n  sourceFilename: 'flow-jsx-example.js',\n});\n\nconsole.log('\\n--- Babel AST (excerpt) ---');\nconsole.log(JSON.stringify(babelAst.body[0], null, 2));\n\nconst simpleCode = `const x = 10;`;\nconst simpleAstTokens = parse(simpleCode, { tokens: true });\nconsole.log('\\n--- Tokens for simple code ---');\nconsole.log(simpleAstTokens.tokens?.map(t => t.value));","lang":"typescript","description":"Demonstrates parsing JavaScript code with Flow and JSX syntax, showing how to toggle between ESTree and Babel AST formats, and control Flow parsing behavior. It also illustrates token extraction."},"warnings":[{"fix":"Ensure `parse` options include `{ babel: true }` if a Babel-compatible AST is required.","message":"The `babel` option defaults to `false`, meaning the parser outputs ASTs conforming to the ESTree specification. If you are integrating with tools that expect Babel's specific AST format (e.g., Babel plugins, some linters), you must explicitly set `babel: true` in the parse options.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"To always parse ambiguous syntax as Flow, pass `{ flow: \"all\" }` in the `parse` options. Otherwise, ensure `@flow` pragmas are present in relevant files.","message":"The `flow` option defaults to `\"detect\"`, which only parses ambiguous syntax as Flow when an `@flow` pragma is present. If you have Flow syntax without a pragma and want it parsed as Flow (e.g., `foo<T>(x)` as a call with type argument), you must set `flow: \"all\"` in the parse options.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Carefully review the release notes and test your integration thoroughly when updating to any new minor version of `hermes-parser`.","message":"As a 0.x release package, `hermes-parser` may introduce breaking changes between minor versions (e.g., 0.30.0 to 0.31.0) without adhering strictly to semver for major bumps. Always review release notes when upgrading.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"If enabling `TransformEnumSyntax` and encountering issues with `flow-enums-runtime`, provide `transformOptions: { TransformEnumSyntax: { enable: true, getRuntime: () => /* your runtime expression */ } }`.","message":"The `transformOptions.TransformEnumSyntax` feature, if enabled, defaults to requiring `flow-enums-runtime`. If this transform is used in an environment where `flow-enums-runtime` is not available or you need a different runtime reference, you must provide a custom `getRuntime` function.","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":"Check `sourceType` (module/script/unambiguous), `flow` (detect/all), and ensure `babel: true` is set if parsing complex JSX or Flow constructions that might require Babel's specific interpretation.","cause":"The parser encountered syntax it does not understand, often due to missing configuration for Flow or JSX, or attempting to parse future JavaScript syntax not yet supported.","error":"SyntaxError: Unexpected token"},{"fix":"Ensure you are using `import { parse } from 'hermes-parser';` for ESM environments. Verify that bundler configurations correctly handle ESM modules. If using TypeScript, check for correct `esModuleInterop` settings.","cause":"This error typically occurs in environments (like Webpack bundles) where CommonJS `require` is used or incorrectly transpiled for an ESM-only package, or when attempting to import a non-existent symbol.","error":"TypeError: hermes_parser__WEBPACK_IMPORTED_MODULE_0__.parse is not a function"},{"fix":"If this is intentional (e.g., for very specific module formats or transpilation targets), you can suppress this error by passing `{ allowReturnOutsideFunction: true }` in the `parse` options. Otherwise, refactor your code to place `return` statements within functions.","cause":"You have `return` statements outside of a function, which is typically a syntax error in JavaScript.","error":"HermesParserError: A 'return' statement can only be used within a function body"}],"ecosystem":"npm"}