Acorn parses JavaScript source into an abstract syntax tree (AST) that follows the ESTree specification. To use it, install acorn, set the language version with ecmaVersion, choose a program mode with sourceType, and pass the source to acorn.parse(). Use acorn-walk to traverse the resulting nodes.
Install Acorn and parse JavaScript
Acorn is a JavaScript-written parser distributed through npm. Its parse() function returns an ESTree-style AST for a complete program. The documented basic workflow is:
-
Install Acorn:
npm install acorn. -
Import it into an ES module:
import * as acorn from "acorn";. CommonJS can also be used in environments that support it. -
Parse source text with an explicit grammar version:
const ast = acorn.parse("1 + 1", { ecmaVersion: 2020 });.Free tools Windows power users keep installed
One-click scans. No signup required.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
The result is a tree of nodes representing the program’s syntax, rather than the source as plain text. Acorn describes its output as an abstract syntax tree specified by ESTree. See the Acorn project README and Acorn documentation.
Choose the grammar version and source type
ecmaVersion
ecmaVersion is required. It can be a year, a plain edition number, or "latest". It governs which JavaScript syntax Acorn accepts, along with related behavior such as strict mode and reserved words. Select a specific year or edition when a project needs predictable parsing; "latest" tracks the newest grammar Acorn supports.
sourceType
sourceType accepts "script", "module", or "commonjs". Use module mode for source containing import or export. CommonJS mode gives the top-level scope function-like behavior; it is distinct from module mode and can better reflect CommonJS-style source. If the code’s mode does not match the selected option, parsing can fail or produce different scope-related behavior.
Acorn implements finalized Stage 4 ECMAScript features. Proposed language features generally require a plugin rather than a built-in option. The supported options and their effects are documented in the Acorn README.
Rank #2
Add source positions or tokens when needed
By default, you can work with the syntax tree without asking Acorn for extra positional metadata. Enable these options when an analysis needs the corresponding information:
-
locations: trueadds line and column location data to nodes. -
ranges: trueadds source-offset ranges, useful when mapping nodes back to spans of text. -
onTokenprovides token callbacks during parsing. -
preserveParensretains parentheses in the AST where applicable.Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallSpecial offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Other parser controls include allowReturnOutsideFunction, allowImportExportEverywhere, allowAwaitOutsideFunction, allowHashBang, and checkPrivateFields. These relax or adjust specific parsing checks; use them only when the input format calls for that behavior. Consult the official option documentation for details.
Traverse an AST with acorn-walk
Acorn parses source; acorn-walk supplies traversal helpers. Its visitors are keyed by ESTree node type, allowing code to respond to constructs such as function declarations or calls without manually descending through every child node.
For example, a visitor for a particular node type can be used to collect matching nodes or inspect their properties. The correct visitor names and traversal methods are part of the Acorn project documentation. Keep parsing and traversal conceptually separate: the parser builds the tree, while the walker visits it.
Parse a single expression or inspect tokens
Use parseExpressionAt for an expression
When the input of interest is one expression beginning at a known offset, use parseExpressionAt(source, offset, options) instead of parsing a complete program. The offset identifies where parsing should begin in the source string.
Recommended Free Tools
Rank #4
Use parser-aware token callbacks for tokens
Acorn also exposes a tokenizer with repeated getToken() calls. Its documentation warns that modern JavaScript tokenization is context-sensitive, so standalone tokenization may not have the context available to the parser. If you need tokens in the context of a parse, prefer parse() with onToken.
Handle syntax errors and incomplete input
Validating ordinary JavaScript
For syntactically invalid input, Acorn reports a SyntaxError with position information and, when available, line and column data. Use those details to identify where parsing stopped or what construct needs attention. A strict parse is appropriate when the goal is to validate JavaScript.
Getting an approximate tree from malformed text
For incomplete or malformed source where a best-effort tree is more useful than rejection, use acorn-loose. It returns a reasonable ESTree approximation; that tree is not proof that the input is valid JavaScript, and downstream analysis should account for missing or inferred structure. See the acorn-loose documentation.
Parse JSX and other syntax extensions
Acorn’s built-in grammar covers finalized ECMAScript features, not every JavaScript-adjacent dialect. For JSX, extend the parser with a plugin such as acorn-jsx. The Acorn documentation also demonstrates plugin use with acorn-bigint.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minuteBest Value
Plugins extend parser behavior by overriding parser methods. That gives them power to add syntax, but also means compatibility should be checked when Acorn internals change. Review the plugin’s compatibility with the Acorn version in the project before upgrading. The extension approach and examples are described in the Acorn documentation.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Print an AST as JSON from the command line
The bin/acorn command-line utility emits a syntax tree as JSON, which can be useful for inspection or automation. Documented flags include:
-
--ecma<version>for selecting an ECMAScript version. -
--modulefor module input. -
--locationsto include location data. -
--allow-hash-bangand--allow-await-outside-functionfor those parsing cases. -
--compactfor compact output,--silentto suppress output, and--helpfor command help.Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Check the installed CLI’s --help output for the exact syntax supported by that version. The command-line options are documented in the Acorn README.
What Acorn is—and is not—established to do
Acorn is a focused parser for turning JavaScript into an ESTree-style AST. The cited project documentation describes it as small and fast, but the available sources do not provide an independent, reproducible performance benchmark or adoption statistic. Do not treat “fastest” as a verified comparison against other parsers.
The npm listing showed Acorn version 8.18.0 in 2026 at the time it was checked; package listings change, so verify the current version on the Acorn npm page before pinning a dependency. That listing does not establish which version is appropriate for a particular application.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

