Acorn JavaScript Parser
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.
Common errors
-
SyntaxError: Unexpected token
cause Parsing modern JavaScript syntax (e.g., `await` outside async, private class fields, nullish coalescing) with an outdated `ecmaVersion` or incorrect `sourceType` option.fixAdjust 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. -
TypeError: acorn.parse is not a function
cause Attempting to use `acorn.parse` directly after an incorrect `import acorn from 'acorn'` (default import) in an ESM context, or an incorrect destructuring of the CommonJS export.fixUse `import * as acorn from 'acorn'` for ESM or `const acorn = require('acorn')` for CommonJS, then access `acorn.parse`. -
SyntaxError: 'return' outside of function
cause A `return` statement is present at the top-level of the parsed code, which is disallowed by default.fixSet the `allowReturnOutsideFunction: true` option in the parser configuration to permit top-level return statements. -
SyntaxError: 'import' and 'export' may only appear at the top level
cause `import` or `export` declarations are used inside a block or non-top-level scope, which is typically disallowed by the ECMAScript specification.fixSet the `allowImportExportEverywhere: true` option in the parser configuration. Note that this might deviate from strict spec compliance.
Warnings
- breaking The `ecmaVersion` option is now explicitly required for `acorn.parse()`. While omitting it might currently work with a warning, it will cause an error in future versions.
- breaking The `ecmaVersion` option in v8 and later prefers year-based numbers (e.g., `2020`) over plain version numbers (e.g., `11`), although older numbers are still supported for backward compatibility. The default `ecmaVersion` also changed to 9 (ES2018) in v7.0.0.
- breaking The `acorn.version` property was removed in favor of accessing the version via `pkg.version`. Code directly referencing `acorn.version` will break.
- gotcha Acorn only implements 'stage 4' (finalized) ECMAScript features. Experimental or 'stage 3' proposals are not natively supported and require specific Acorn plugins (e.g., `acorn-jsx`, `acorn-bigint`).
- gotcha The default behavior for `allowReserved` (allowing reserved words as identifiers) changes based on `ecmaVersion`. It defaults to `true` for `ecmaVersion` 3 but `false` for higher versions, which can lead to parsing errors for older codebases expecting `allowReserved: true`.
- gotcha Using `sourceType: 'commonjs'` is not allowed when `allowAwaitOutsideFunction: true`, as top-level await is a module feature.
Install
-
npm install acorn -
yarn add acorn -
pnpm add acorn
Imports
- acorn
import acorn from 'acorn'
import * as acorn from 'acorn'
- acorn
const { parse } = require('acorn')const acorn = require('acorn') - Parser
import { default as Parser } from 'acorn'import { Parser } from 'acorn'
Quickstart
import { parse } from 'acorn';
const code = `
function greet(name = 'World') {
console.log(`Hello, ${name}!`);
}
greet('Registry');
greet();
`;
try {
const ast = parse(code, {
ecmaVersion: 2022, // Specify the ECMAScript version
sourceType: 'module', // Or 'script', or 'commonjs'
locations: true, // Attach line/column location info to nodes
});
console.log('AST generated successfully:');
console.log(JSON.stringify(ast, null, 2));
// Example of accessing a node
if (ast.body[0].type === 'FunctionDeclaration') {
console.log(`\nFirst function name: ${ast.body[0].id.name}`);
}
} catch (error) {
console.error('Parsing error:', error.message);
console.error('Position:', error.pos, 'Line:', error.loc.line, 'Column:', error.loc.column);
}