Acorn is a compact, high-performance JavaScript parser implemented entirely in JavaScript. It parses ECMAScript code into an Abstract Syntax Tree (AST) that conforms to the ESTree specification. The current stable version is 8.16.0, as of February 2026, with frequent releases to support the latest ECMAScript features and bug fixes. Acorn differentiates itself by its minimal footprint and speed, serving as a fundamental component in many JavaScript tooling projects, including linters (like ESLint), bundlers, and transpilers. It strictly implements 'stage 4' (finalized) ECMAScript features, requiring plugins for experimental syntax.
npm install acornVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates parsing a JavaScript code snippet using Acorn, specifying ECMAScript version and source type, and then logging the generated AST and handling potential syntax errors.
Always provide the `ecmaVersion` option (e.g., `{ ecmaVersion: 2022 }` or `{ ecmaVersion: 'latest' }`).Update `ecmaVersion` to use year-based numbers like `2022` or the string `'latest'` for modern JavaScript parsing.
Retrieve the package version using `require('acorn/package.json').version` in CommonJS or `import { version } from 'acorn/package.json'` in ESM.For parsing experimental syntax, identify and integrate the appropriate Acorn plugin using the `Parser.extend()` method.
Explicitly set `allowReserved: true` in options if parsing older JavaScript code that uses reserved words as identifiers with a modern `ecmaVersion`.
Ensure `sourceType` is set to `'module'` when `allowAwaitOutsideFunction` is enabled for top-level `await` expressions.
Adjust the `ecmaVersion` option to a sufficiently high year (e.g., `2022` or `'latest'`) and set `sourceType: 'module'` if parsing ES modules, or add appropriate plugins for non-standard syntax.
Use `import * as acorn from 'acorn'` for ESM or `const acorn = require('acorn')` for CommonJS, then access `acorn.parse`.Set the `allowReturnOutsideFunction: true` option in the parser configuration to permit top-level return statements.
Set the `allowImportExportEverywhere: true` option in the parser configuration. Note that this might deviate from strict spec compliance.