Registry / serialization / esprima

esprima

JSON →
library4.0.1pypypi✓ verified 86d ago

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 esprima
INSTALL
IMPORT
SIG · ESPRIMA
E
esprima
serializationpythonv4.0.1
Install
3.5s avg
Import
2062ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 2.175s · 19.7MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.5s · import 1.950s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
from esprima import parse
The primary function to parse JavaScript/ECMAScript code.
EsprimaException
from esprima.error import EsprimaException
For handling parsing errors.

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.

import esprima js_code = "var answer = 42; console.log(answer);" try: # Parse the JavaScript code tree = esprima.parse(js_code, options={'loc': True, 'range': True}) # The 'tree' object is an AST. It can be converted to a dictionary. ast_dict = tree.to_dict() # Print a part of the AST (e.g., the first statement's type) if ast_dict and 'body' in ast_dict and len(ast_dict['body']) > 0: print(f"Parsed statement type: {ast_dict['body'][0]['type']}") # Example of accessing tokens (if options={'tokens': True} was used) # tokens = esprima.tokenize(js_code) # print(f"First token: {tokens[0].to_dict()}") except esprima.error.EsprimaException as e: print(f"Parsing error: {e}")
Debug
Known issues
breakingMajor API changes occurred between version 3.x and 4.x. If upgrading from an older major version, review the GitHub README for updated usage patterns. Direct migration without code changes is unlikely to work.
fix
Consult the `esprima-python` GitHub repository's README and changelog for detailed API changes when upgrading from Esprima Python 3.x to 4.x.
affects: 3.x to 4.x
breakingThe `AST.to_json()` method was removed in version 4.0.0. Use `AST.to_dict()` instead for converting the AST object to a Python dictionary, which can then be serialized to JSON using Python's `json` module if needed.
fix
Replace calls to `ast_object.to_json()` with `ast_object.to_dict()`. If JSON output is required, use `json.dumps(ast_object.to_dict())`.
affects: >=4.0.0
gotchaEsprima Python generates an AST that conforms to the SpiderMonkey Parser API's AST format for ECMAScript (JavaScript). This means the structure and node types in the resulting AST (e.g., from `tree.to_dict()`) will reflect JavaScript syntax, not Python's native AST or common Pythonic conventions. Users familiar with Python's `ast` module might find the structure different.
fix
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.
affects: All versions
Errors
Common errors & fixes
esprima.error_handler.Error: Line X: Unexpected token
The JavaScript code contains ES Module syntax (e.g., `import` or `export` statements) but is being parsed using `parseScript()` instead of `parseModule()`.
fix
Use `esprima.parseModule(code)` instead of `esprima.parseScript(code)` to parse JavaScript modules.
esprima.error_handler.Error: Line X: Unexpected token
The JavaScript code includes JSX syntax, but the `jsx` parsing option is not enabled.
fix
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 (`<></>`).
ModuleNotFoundError: No module named 'esprima'
The `esprima` Python package is not installed in the current Python environment.
fix
Install the package using pip: `pip install esprima`.
esprima.error_handler.Error: Line X: Unexpected token (
An anonymous function declaration (e.g., `function () {}`) is being parsed as a statement, but `esprima` expects a named function declaration in that context.
fix
Wrap the anonymous function in parentheses to make it a function expression: `esprima.parseScript('(function () {})')`.
Upgrade
Version history
4.0.1latest on PyPI · released Aug 24, 2018
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
12
Resources
esprima — pip install esprima · libregistry