{"id":10474,"library":"acorn-node","title":"Acorn with Node.js Syntax Plugins","description":"acorn-node is a JavaScript parser library that bundles the core Acorn parser with a set of preloaded plugins, providing syntax parity with recent Node.js versions. This includes support for features like BigInt, numeric separators, public and private class fields (instance and static), dynamic `import()`, `import.meta`, and `export * as ns from`. The package sets default options like `ecmaVersion: 2019`, `allowHashBang: true`, and `allowReturnOutsideFunction: true` to align with Node.js module semantics. The current stable version is 2.0.1. Releases appear to follow Acorn's updates and Node.js syntax advancements, with minor patch releases addressing specific parsing issues. Its key differentiator is providing a ready-to-use Acorn instance capable of parsing modern Node.js JavaScript without requiring manual plugin configuration, including compatibility for older Node.js versions via Bublé-compiled plugins.","status":"active","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/browserify/acorn-node","tags":["javascript","acorn","browserify","parser"],"install":[{"cmd":"npm install acorn-node","lang":"bash","label":"npm"},{"cmd":"yarn add acorn-node","lang":"bash","label":"yarn"},{"cmd":"pnpm add acorn-node","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core JavaScript parser library that acorn-node extends and configures.","package":"acorn","optional":false},{"reason":"Provides BigInt syntax parsing.","package":"acorn-bigint","optional":false},{"reason":"Adds support for public and private class instance and static fields.","package":"acorn-class-fields","optional":false},{"reason":"Enables parsing of dynamic `import()` expressions.","package":"acorn-dynamic-import","optional":false},{"reason":"Adds support for `export * as ns from` syntax.","package":"acorn-export-ns-from","optional":false},{"reason":"Provides parsing for the `import.meta` property.","package":"acorn-import-meta","optional":false},{"reason":"Adds support for numeric separator syntax (e.g., `1_000_000`).","package":"acorn-numeric-separator","optional":false}],"imports":[{"note":"The primary export is the configured Acorn parser. While CommonJS `require('acorn-node')` is also common, using `import * as acorn from 'acorn-node'` is standard for ESM contexts.","wrong":"const acorn = require('acorn')","symbol":"acorn","correct":"import * as acorn from 'acorn-node'"},{"note":"The AST walker is available from a sub-path. It exposes various walking methods (e.g., `simple`, `full`, `ancestor`) rather than a single `walk` function.","wrong":"const { walk } = require('acorn-node')","symbol":"walk","correct":"import { simple as walkSimple } from 'acorn-node/walk'"},{"note":"Node types are implicitly part of the AST. Specifically, dynamic import nodes changed from `Import` to `ImportExpression` in v2.0.0, aligning with Acorn's standard AST.","wrong":"import { Import } from 'acorn-node'","symbol":"NodeTypes","correct":"import { Node } from 'acorn-node'"}],"quickstart":{"code":"import * as acorn from 'acorn-node';\nimport { simple as walkSimple } from 'acorn-node/walk';\n\nconst code = `\n  class MyClass {\n    #privateField = 1;\n    static publicStaticField = 2;\n    constructor() {\n      console.log(this.#privateField);\n    }\n  }\n  const bigNum = 1_000_000_000_000_000n;\n  const dynamicImport = import('./module.js');\n  export * as ns from './ns.mjs';\n`;\n\ntry {\n  const ast = acorn.parse(code, { sourceType: 'module' });\n  console.log('AST parsed successfully. Root node type:', ast.type);\n\n  let foundClassFields = 0;\n  walkSimple(ast, {\n    PropertyDefinition(node) {\n      if (node.key.type === 'PrivateIdentifier' || node.static) {\n        foundClassFields++;\n      }\n    },\n    ImportExpression(node) {\n      console.log('Found dynamic import:', node.source.value);\n    }\n  });\n  console.log(`Found ${foundClassFields} class field definitions.`);\n\n} catch (e) {\n  console.error('Parsing error:', e.message);\n}\n","lang":"javascript","description":"This example demonstrates parsing a JavaScript code snippet using acorn-node, including modern features like private class fields, static class fields, numeric separators, BigInts, dynamic `import()`, and `export * as ns from`. It then uses the bundled `walk` utility to traverse the AST and identify specific node types."},"warnings":[{"fix":"Update AST traversal logic to check for `ImportExpression` nodes instead of `Import` nodes when handling dynamic imports.","message":"The node type for dynamic `import()` expressions changed from `Import` to `ImportExpression`. This aligns with the standard Acorn/ESTree AST specification.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Be aware of the `Import` vs `ImportExpression` distinction in this version range. If migrating to v2.0.0 or higher, adjust code to expect `ImportExpression`.","message":"When `acorn-node` upgraded to Acorn v7 (in `acorn-node` v1.8.0), it initially maintained the `Import` node type for dynamic imports for backwards compatibility, deviating from Acorn v7's `ImportExpression`. This could lead to confusion if integrating with other tools expecting standard Acorn v7 ASTs.","severity":"gotcha","affected_versions":">=1.8.0 <2.0.0"},{"fix":"Upgrade to v2.0.0 or higher to correctly handle escape sequences in `import.meta`. If unable to upgrade, avoid using escape sequences in `import.meta` properties.","message":"Prior to v2.0.0, `acorn-node` rejected escape sequences in `import.meta` which could lead to unexpected parsing errors for certain `import.meta` usages.","severity":"gotcha","affected_versions":"<2.0.0"},{"fix":"Avoid `acorn-node` version 1.8.1. Upgrade to 1.8.2 or later, or downgrade to 1.8.0.","message":"A regression in `import.meta` parsing was introduced and subsequently reverted. This could cause crashes or incorrect parsing of `import.meta` properties.","severity":"breaking","affected_versions":"1.8.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you are using `acorn-node` (current version 2.0.1 or higher) and not `acorn` directly, as `acorn-node` preloads the necessary plugins. Also, verify `sourceType` is correctly set in parsing options (e.g., `'module'` for ES modules).","cause":"Attempting to parse modern JavaScript syntax (e.g., class fields, BigInt) with a plain `acorn` instance or an outdated `acorn-node` version.","error":"SyntaxError: Unexpected token (XX:YY)"},{"fix":"Update your AST traversal logic to look for `ImportExpression` nodes instead of `Import` nodes. Example: `if (node.type === 'ImportExpression') { ... }`.","cause":"Code expecting `Import` nodes for dynamic imports when using `acorn-node` v2.0.0 or later, which switched to `ImportExpression`.","error":"TypeError: Cannot read properties of undefined (reading 'type') or similar errors when traversing AST for dynamic imports."},{"fix":"For Node.js environments, ensure consistency: either use `require()` exclusively (and save files as `.js` if `type: module` isn't in `package.json`) or use `import` exclusively (and save files as `.mjs` or ensure `type: module` in `package.json`). For browser environments, use bundlers or ensure script tags have `type='module'` for ESM.","cause":"Mixing CommonJS `require()` and ESM `import` statements or running code in an environment that doesn't support the chosen module system.","error":"ReferenceError: require is not defined or SyntaxError: Cannot use import statement outside a module"}],"ecosystem":"npm"}