{"id":10846,"library":"espree","title":"Espree JavaScript Parser","description":"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.","status":"active","version":"11.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/eslint/js","tags":["javascript","ast","ecmascript","parser","syntax","acorn","typescript"],"install":[{"cmd":"npm install espree","lang":"bash","label":"npm"},{"cmd":"yarn add espree","lang":"bash","label":"yarn"},{"cmd":"pnpm add espree","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Espree is exported as a namespace module in ESM contexts. Using a default import will result in `undefined`.","wrong":"import espree from 'espree';","symbol":"espree","correct":"import * as espree from 'espree';"},{"note":"The `parse` function is a property of the `espree` namespace object, not a direct named export.","wrong":"import { parse } from 'espree';\nconst ast = parse(code, options);","symbol":"espree.parse","correct":"import * as espree from 'espree';\nconst ast = espree.parse(code, options);"},{"note":"For CommonJS environments, `require()` is the standard way to import the module. Using ESM import syntax directly in CJS will fail.","wrong":"import * as espree from 'espree';","symbol":"CommonJS require","correct":"const espree = require('espree');\nconst ast = espree.parse(code, options);"}],"quickstart":{"code":"import * as espree from \"espree\";\n\nconst code = `\n  // This is a comment\n  function greet(name: string) {\n    console.log(`Hello, ${name}!`);\n  }\n  greet(\"World\");\n`;\n\ntry {\n  // Parse with options for range, location, comments, and tokens\n  const ast = espree.parse(code, {\n    ecmaVersion: \"latest\", // Use the latest supported ECMAScript version\n    sourceType: \"module\",  // Or \"script\"\n    range: true,           // Include start/end indices for each node\n    loc: true,             // Include line/column for each node\n    comment: true,         // Include comments in the AST\n    tokens: true           // Include tokens in the AST\n  });\n\n  console.log(\"--- AST (truncated) ---\");\n  // Only log a portion for brevity, the full AST can be very large\n  console.log(JSON.stringify(ast.body.slice(0, 1), null, 2));\n\n  console.log(\"\\n--- First 3 Tokens ---\");\n  if (ast.tokens) {\n    ast.tokens.slice(0, 3).forEach(token => console.log(token));\n  } else {\n    console.log(\"Tokens not generated. Ensure 'tokens: true' option is set.\");\n  }\n\n  console.log(\"\\n--- Comments ---\");\n  if (ast.comments) {\n    ast.comments.forEach(comment => console.log(comment));\n  } else {\n    console.log(\"Comments not generated. Ensure 'comment: true' option is set.\");\n  }\n\n} catch (e: any) {\n  console.error(\"Error parsing code:\", e.message);\n}","lang":"typescript","description":"Demonstrates parsing JavaScript code into an AST and extracting tokens and comments using various options, including `ecmaVersion: 'latest'` and `sourceType: 'module'`."},"warnings":[{"fix":"Upgrade your Node.js environment to `^20.19.0 || ^22.13.0 || >=24`.","message":"Espree v11.0.0 and later require specific Node.js versions. Older Node.js runtimes will fail.","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"Update any code that relies on the exact `range` of the `Program` node to account for its new, expanded span.","message":"Starting with v11.0.0, the `Program` node's `range` property now spans the entire source text, including any leading or trailing whitespace/comments.","severity":"breaking","affected_versions":">=11.0.0"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Set `ecmaVersion: 'latest'` or specify a modern year (e.g., `ecmaVersion: 2024`) in the parse options object.","cause":"The `ecmaVersion` option was not set correctly or is too old to support the JavaScript syntax being parsed.","error":"SyntaxError: The keyword 'await' is reserved (or similar for other modern syntax features)"},{"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`.","cause":"Attempting to use CommonJS `require()` syntax in an ECMAScript Module (ESM) file.","error":"ReferenceError: require is not defined in ES module scope"},{"fix":"For ESM, correctly import the module as a namespace: `import * as espree from \"espree\";`. Then, call `espree.parse(code)`.","cause":"Incorrect ESM import style. Espree is exported as a namespace object, not with named exports for its functions.","error":"TypeError: espree.parse is not a function"}],"ecosystem":"npm"}