Registry / serialization / acorn
library0.8.1jsnpmunverified

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 acorn
INSTALL
IMPORT
SIG · ACORN
A
acorn
serializationjavascriptv0.8.1
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.

acorn
import * as acorn from 'acorn'
import acorn from 'acorn'
Acorn exports its API as a namespace object; a default import is incorrect. This is the preferred ESM import.
acorn
const acorn = require('acorn')
const { parse } = require('acorn')
For CommonJS, `require('acorn')` returns the full API object. Destructuring directly may miss other top-level exports if not careful.
Parser
import { Parser } from 'acorn'
import { default as Parser } from 'acorn'
The `Parser` class is a named export, primarily used for extending Acorn with plugins.

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.

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); }
Debug
Known issues
breakingThe `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.
fix
Always provide the `ecmaVersion` option (e.g., `{ ecmaVersion: 2022 }` or `{ ecmaVersion: 'latest' }`).
affects: >=8.0.0
breakingThe `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.
fix
Update `ecmaVersion` to use year-based numbers like `2022` or the string `'latest'` for modern JavaScript parsing.
affects: >=8.0.0
breakingThe `acorn.version` property was removed in favor of accessing the version via `pkg.version`. Code directly referencing `acorn.version` will break.
fix
Retrieve the package version using `require('acorn/package.json').version` in CommonJS or `import { version } from 'acorn/package.json'` in ESM.
affects: >=8.0.0
gotchaAcorn 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`).
fix
For parsing experimental syntax, identify and integrate the appropriate Acorn plugin using the `Parser.extend()` method.
affects: >=0.4.0
gotchaThe 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`.
fix
Explicitly set `allowReserved: true` in options if parsing older JavaScript code that uses reserved words as identifiers with a modern `ecmaVersion`.
affects: >=0.4.0
gotchaUsing `sourceType: 'commonjs'` is not allowed when `allowAwaitOutsideFunction: true`, as top-level await is a module feature.
fix
Ensure `sourceType` is set to `'module'` when `allowAwaitOutsideFunction` is enabled for top-level `await` expressions.
affects: >=8.0.0
Errors
Common errors & fixes
SyntaxError: Unexpected token
Parsing modern JavaScript syntax (e.g., `await` outside async, private class fields, nullish coalescing) with an outdated `ecmaVersion` or incorrect `sourceType` option.
fix
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.
TypeError: acorn.parse is not a function
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.
fix
Use `import * as acorn from 'acorn'` for ESM or `const acorn = require('acorn')` for CommonJS, then access `acorn.parse`.
SyntaxError: 'return' outside of function
A `return` statement is present at the top-level of the parsed code, which is disallowed by default.
fix
Set 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
`import` or `export` declarations are used inside a block or non-top-level scope, which is typically disallowed by the ECMAScript specification.
fix
Set the `allowImportExportEverywhere: true` option in the parser configuration. Note that this might deviate from strict spec compliance.
Upgrade
Version history
0.8.1latest on npm
Audit
Dependencies
acorn-walkoptionalCommonly used for traversing the AST generated by Acorn, providing utilities for visiting nodes.
acorn-looseoptionalProvides an error-tolerant parser for situations where syntax errors should not halt parsing, often used for IDEs or tools that need to process incomplete code.
nodejsrequiredRuntime environment for Acorn, specified in engine requirements.
Agent activity
55 hits · last 30 days
node
46
OpenAI (training)
1
Resources