Esprima Python is a Python port of the Esprima ECMAScript parser. It provides an ECMAScript (JavaScript) parsing infrastructure for multipurpose analysis in Python, generating a syntax tree that adheres to the SpiderMonkey AST format. The current version is 4.0.1, with releases typically following updates to the upstream JavaScript Esprima project or significant internal Python improvements.
pip install esprimaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates parsing a simple JavaScript string using `esprima.parse()` and then converting the resulting Abstract Syntax Tree (AST) object to a Python dictionary for inspection. It also shows how to catch parsing-specific exceptions.
Consult the `esprima-python` GitHub repository's README and changelog for detailed API changes when upgrading from Esprima Python 3.x to 4.x.
Replace calls to `ast_object.to_json()` with `ast_object.to_dict()`. If JSON output is required, use `json.dumps(ast_object.to_dict())`.
When working with the AST, refer to the SpiderMonkey Parser API documentation or `esprima-python` examples to understand the expected node types and properties. Do not expect a Python-native AST structure.
Use `esprima.parseModule(code)` instead of `esprima.parseScript(code)` to parse JavaScript modules.
Pass `{"jsx": True}` in the options dictionary to enable JSX parsing: `esprima.parseModule(code, {"jsx": True})` or `esprima.parseScript(code, {"jsx": True})`. Note that `esprima-python` (version 4.0.1) may not fully support all modern JSX features, such as React fragments (`<></>`).Install the package using pip: `pip install esprima`.
Wrap the anonymous function in parentheses to make it a function expression: `esprima.parseScript('(function () {})')`.No dependency data recorded yet.