{"id":15505,"library":"acorn","title":"Acorn JavaScript Parser","description":"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.","status":"active","version":"8.16.0","language":"javascript","source_language":"en","source_url":"https://github.com/acornjs/acorn","tags":["javascript","typescript"],"install":[{"cmd":"npm install acorn","lang":"bash","label":"npm"},{"cmd":"yarn add acorn","lang":"bash","label":"yarn"},{"cmd":"pnpm add acorn","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Commonly used for traversing the AST generated by Acorn, providing utilities for visiting nodes.","package":"acorn-walk","optional":true},{"reason":"Provides 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.","package":"acorn-loose","optional":true},{"reason":"Runtime environment for Acorn, specified in engine requirements.","package":"nodejs","optional":false}],"imports":[{"note":"Acorn exports its API as a namespace object; a default import is incorrect. This is the preferred ESM import.","wrong":"import acorn from 'acorn'","symbol":"acorn","correct":"import * as acorn from 'acorn'"},{"note":"For CommonJS, `require('acorn')` returns the full API object. Destructuring directly may miss other top-level exports if not careful.","wrong":"const { parse } = require('acorn')","symbol":"acorn","correct":"const acorn = require('acorn')"},{"note":"The `Parser` class is a named export, primarily used for extending Acorn with plugins.","wrong":"import { default as Parser } from 'acorn'","symbol":"Parser","correct":"import { Parser } from 'acorn'"}],"quickstart":{"code":"import { parse } from 'acorn';\n\nconst code = `\n  function greet(name = 'World') {\n    console.log(`Hello, ${name}!`);\n  }\n  greet('Registry');\n  greet();\n`;\n\ntry {\n  const ast = parse(code, {\n    ecmaVersion: 2022, // Specify the ECMAScript version\n    sourceType: 'module', // Or 'script', or 'commonjs'\n    locations: true, // Attach line/column location info to nodes\n  });\n  console.log('AST generated successfully:');\n  console.log(JSON.stringify(ast, null, 2));\n\n  // Example of accessing a node\n  if (ast.body[0].type === 'FunctionDeclaration') {\n    console.log(`\\nFirst function name: ${ast.body[0].id.name}`);\n  }\n} catch (error) {\n  console.error('Parsing error:', error.message);\n  console.error('Position:', error.pos, 'Line:', error.loc.line, 'Column:', error.loc.column);\n}","lang":"typescript","description":"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."},"warnings":[{"fix":"Always provide the `ecmaVersion` option (e.g., `{ ecmaVersion: 2022 }` or `{ ecmaVersion: 'latest' }`).","message":"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.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Update `ecmaVersion` to use year-based numbers like `2022` or the string `'latest'` for modern JavaScript parsing.","message":"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.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Retrieve the package version using `require('acorn/package.json').version` in CommonJS or `import { version } from 'acorn/package.json'` in ESM.","message":"The `acorn.version` property was removed in favor of accessing the version via `pkg.version`. Code directly referencing `acorn.version` will break.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"For parsing experimental syntax, identify and integrate the appropriate Acorn plugin using the `Parser.extend()` method.","message":"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`).","severity":"gotcha","affected_versions":">=0.4.0"},{"fix":"Explicitly set `allowReserved: true` in options if parsing older JavaScript code that uses reserved words as identifiers with a modern `ecmaVersion`.","message":"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`.","severity":"gotcha","affected_versions":">=0.4.0"},{"fix":"Ensure `sourceType` is set to `'module'` when `allowAwaitOutsideFunction` is enabled for top-level `await` expressions.","message":"Using `sourceType: 'commonjs'` is not allowed when `allowAwaitOutsideFunction: true`, as top-level await is a module feature.","severity":"gotcha","affected_versions":">=8.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"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.","cause":"Parsing modern JavaScript syntax (e.g., `await` outside async, private class fields, nullish coalescing) with an outdated `ecmaVersion` or incorrect `sourceType` option.","error":"SyntaxError: Unexpected token"},{"fix":"Use `import * as acorn from 'acorn'` for ESM or `const acorn = require('acorn')` for CommonJS, then access `acorn.parse`.","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.","error":"TypeError: acorn.parse is not a function"},{"fix":"Set the `allowReturnOutsideFunction: true` option in the parser configuration to permit top-level return statements.","cause":"A `return` statement is present at the top-level of the parsed code, which is disallowed by default.","error":"SyntaxError: 'return' outside of function"},{"fix":"Set the `allowImportExportEverywhere: true` option in the parser configuration. Note that this might deviate from strict spec compliance.","cause":"`import` or `export` declarations are used inside a block or non-top-level scope, which is typically disallowed by the ECMAScript specification.","error":"SyntaxError: 'import' and 'export' may only appear at the top level"}],"ecosystem":"npm"}