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.
npm install hermes-parserVerified import paths — ran on the pinned version, not inferred.
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.
Ensure `parse` options include `{ babel: true }` if a Babel-compatible AST is required.To always parse ambiguous syntax as Flow, pass `{ flow: "all" }` in the `parse` options. Otherwise, ensure `@flow` pragmas are present in relevant files.Carefully review the release notes and test your integration thoroughly when updating to any new minor version of `hermes-parser`.
If enabling `TransformEnumSyntax` and encountering issues with `flow-enums-runtime`, provide `transformOptions: { TransformEnumSyntax: { enable: true, getRuntime: () => /* your runtime expression */ } }`.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.
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.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.