{"id":10801,"library":"esast-util-from-estree","title":"ESTree to ESAST Transformer","description":"esast-util-from-estree is a utility within the unified collective's ecosystem, specifically designed to convert an ESTree-compliant Abstract Syntax Tree (AST) into an esast, which is a unist-compliant AST for ECMAScript. Currently stable at version 2.0.0, this package plays a crucial role in enabling unist utilities to process JavaScript ASTs. It ensures that the transformed nodes are plain JSON objects, incorporates unist positional information, normalizes specific fields like `.bigint`, and prunes discouraged or redundant fields such as `attributes` and `selfClosing` from JSXOpeningFragment nodes to ensure strict unist compatibility. The package maintains the common release cadence of the unified collective, with major versions like v2.0.0 introducing breaking changes such as the shift to ESM-only distribution and requiring Node.js 16 or newer. Its primary differentiator is providing a robust, standard-compliant bridge between the ESTree format, typically generated by parsers like Acorn, and the unist specification.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/syntax-tree/esast-util-from-estree","tags":["javascript","esast","esast-util","util","utility","recma","estree","typescript"],"install":[{"cmd":"npm install esast-util-from-estree","lang":"bash","label":"npm"},{"cmd":"yarn add esast-util-from-estree","lang":"bash","label":"yarn"},{"cmd":"pnpm add esast-util-from-estree","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is ESM-only since v2.0.0. There is no default export.","wrong":"const fromEstree = require('esast-util-from-estree')","symbol":"fromEstree","correct":"import { fromEstree } from 'esast-util-from-estree'"},{"note":"For Deno and browser environments, use esm.sh. Append `?bundle` for browser bundling.","symbol":"fromEstree (Deno/Browser)","correct":"import {fromEstree} from 'https://esm.sh/esast-util-from-estree@2'"},{"note":"TypeScript types are shipped with the package for configuring the transformation.","symbol":"Options (Type)","correct":"import type { Options } from 'esast-util-from-estree'"}],"quickstart":{"code":"import { parse } from 'acorn';\nimport { fromEstree } from 'esast-util-from-estree';\n\n// Make acorn support comments and positional info.\n/** @type {Array<import('acorn').Comment>} */\nconst comments = [];\n/** @type {import('estree').Program} */\n// @ts-expect-error: acorn looks like estree.\nconst estree = parse(\n  'export function x() { /* Something senseless */ console.log(/(?:)/ + 1n) }',\n  {\n    sourceType: 'module',\n    ecmaVersion: 'latest',\n    locations: true,\n    onComment: comments\n  }\n);\nestree.comments = comments;\n\n// Transform the estree to an esast. Note: v2.0.0+ requires using the return value.\nconst esast = fromEstree(estree);\n\nconsole.log(JSON.stringify(esast, null, 2));\n","lang":"typescript","description":"This quickstart demonstrates parsing a JavaScript code string with Acorn to create an ESTree, then transforming that ESTree into an esast using `esast-util-from-estree`."},"warnings":[{"fix":"Upgrade your Node.js environment to version 16 or newer, or stick to an older major version of the package if you must support older Node.js versions.","message":"Version 2.0.0 of esast-util-from-estree requires Node.js 16 or higher.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Migrate your project to use ES modules (`import` statements) or use dynamic `import()` for this package. Ensure your `package.json` has `\"type\": \"module\"` if you're using ESM in Node.js.","message":"Since version 2.0.0, this package is ESM-only. CommonJS `require()` is no longer supported.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Change your code from `fromEstree(estree)` to `const esast = fromEstree(estree)` to correctly capture the transformed AST.","message":"As of v2.0.0, the `fromEstree` function no longer mutates the input ESTree. You must use the return value of the function.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Only import directly from the package root: `import { fromEstree } from 'esast-util-from-estree'`.","message":"The package uses the `exports` field in its `package.json`. Directly importing internal paths (e.g., `esast-util-from-estree/lib/something`) is not supported and may break.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Convert your file to an ES module using `import { fromEstree } from 'esast-util-from-estree'` and ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json`).","cause":"Attempting to use `require()` to import esast-util-from-estree, which is an ESM-only package since v2.0.0.","error":"ERR_REQUIRE_ESM: require() of ES Module .../node_modules/esast-util-from-estree/index.js from .../my-script.js not supported."},{"fix":"Ensure you are using the return value: `const esast = fromEstree(estree)`. Do not expect `estree` to be transformed in place.","cause":"The `fromEstree` function in v2.0.0+ no longer mutates its input; old code might be implicitly relying on mutation.","error":"TypeError: Cannot read properties of undefined (reading 'type') at fromEstree (...)"},{"fix":"Use a named import as there is no default export: `import { fromEstree } from 'esast-util-from-estree'`.","cause":"Attempting to import `fromEstree` as a default export or with an incorrect named import.","error":"TypeError: (0 , esast_util_from_estree_1.fromEstree) is not a function"}],"ecosystem":"npm"}