Registry / web-framework / espree

espree

JSON →
library11.2.0jsnpmunverified

Espree is a high-performance, Esprima-compatible JavaScript parser built upon the modular Acorn parsing library. It is the default parser used by ESLint and is designed to produce Abstract Syntax Trees (ASTs) that are highly consistent with the Esprima API, making it a drop-in replacement in many scenarios. The package is currently at stable version 11.2.0 and receives active maintenance with frequent updates, typically on a monthly or bi-monthly schedule for minor and patch releases. Key differentiators include its robust support for all modern ECMAScript features up to ES2026 (ECMAScript 17), comprehensive parsing options for fine-grained control over AST output (including ranges, locations, comments, and tokens), and official TypeScript type definitions since v11.1.0, enhancing developer experience for TypeScript projects.

npm install espree
INSTALL
IMPORT
SIG · ESPREE
E
espree
web-frameworkjavascriptv11.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
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
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

espree
import * as espree from 'espree';
import espree from 'espree';
Espree is exported as a namespace module in ESM contexts. Using a default import will result in `undefined`.
espree.parse
import * as espree from 'espree'; const ast = espree.parse(code, options);
import { parse } from 'espree'; const ast = parse(code, options);
The `parse` function is a property of the `espree` namespace object, not a direct named export.
CommonJS require
const espree = require('espree'); const ast = espree.parse(code, options);
import * as espree from 'espree';
For CommonJS environments, `require()` is the standard way to import the module. Using ESM import syntax directly in CJS will fail.

Demonstrates parsing JavaScript code into an AST and extracting tokens and comments using various options, including `ecmaVersion: 'latest'` and `sourceType: 'module'`.

import * as espree from "espree"; const code = ` // This is a comment function greet(name: string) { console.log(`Hello, ${name}!`); } greet("World"); `; try { // Parse with options for range, location, comments, and tokens const ast = espree.parse(code, { ecmaVersion: "latest", // Use the latest supported ECMAScript version sourceType: "module", // Or "script" range: true, // Include start/end indices for each node loc: true, // Include line/column for each node comment: true, // Include comments in the AST tokens: true // Include tokens in the AST }); console.log("--- AST (truncated) ---"); // Only log a portion for brevity, the full AST can be very large console.log(JSON.stringify(ast.body.slice(0, 1), null, 2)); console.log("\n--- First 3 Tokens ---"); if (ast.tokens) { ast.tokens.slice(0, 3).forEach(token => console.log(token)); } else { console.log("Tokens not generated. Ensure 'tokens: true' option is set."); } console.log("\n--- Comments ---"); if (ast.comments) { ast.comments.forEach(comment => console.log(comment)); } else { console.log("Comments not generated. Ensure 'comment: true' option is set."); } } catch (e: any) { console.error("Error parsing code:", e.message); }
Debug
Known issues
breakingEspree v11.0.0 and later require specific Node.js versions. Older Node.js runtimes will fail.
fix
Upgrade your Node.js environment to `^20.19.0 || ^22.13.0 || >=24`.
affects: >=11.0.0
breakingStarting with v11.0.0, the `Program` node's `range` property now spans the entire source text, including any leading or trailing whitespace/comments.
fix
Update any code that relies on the exact `range` of the `Program` node to account for its new, expanded span.
affects: >=11.0.0
gotchaThe `espree.parse()` and `espree.tokenize()` methods handle the `tokens` and `comment` options differently. For `parse()`, you must explicitly set `tokens: true` and `comment: true` in the options to receive `tokens` and `comments` properties on the resulting AST. However, `tokenize()` implicitly enables `tokens: true` regardless of the option's value to ensure tokens are always returned.
fix
When using `espree.parse()`, always ensure `tokens: true` and `comment: true` are set in the options if you need these details in the AST output.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: The keyword 'await' is reserved (or similar for other modern syntax features)
The `ecmaVersion` option was not set correctly or is too old to support the JavaScript syntax being parsed.
fix
Set `ecmaVersion: 'latest'` or specify a modern year (e.g., `ecmaVersion: 2024`) in the parse options object.
ReferenceError: require is not defined in ES module scope
Attempting to use CommonJS `require()` syntax in an ECMAScript Module (ESM) file.
fix
If your project uses ESM, use `import * as espree from "espree";` to import the library. Ensure your `package.json` has `"type": "module"` or files end with `.mjs`.
TypeError: espree.parse is not a function
Incorrect ESM import style. Espree is exported as a namespace object, not with named exports for its functions.
fix
For ESM, correctly import the module as a namespace: `import * as espree from "espree";`. Then, call `espree.parse(code)`.
Upgrade
Version history
11.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
espree — npm install espree · libregistry