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.
npm install acorn-nodeVerified import paths — ran on the pinned version, not inferred.
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.
Update AST traversal logic to check for `ImportExpression` nodes instead of `Import` nodes when handling dynamic imports.
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`.
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.
Avoid `acorn-node` version 1.8.1. Upgrade to 1.8.2 or later, or downgrade to 1.8.0.
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).
Update your AST traversal logic to look for `ImportExpression` nodes instead of `Import` nodes. Example: `if (node.type === 'ImportExpression') { ... }`.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.