{"id":10803,"library":"esast-util-from-js","title":"Parse JavaScript to ESAST/ESTree","description":"esast-util-from-js is a utility within the unified ecosystem designed to parse JavaScript code strings into an esast syntax tree, which is an extension of the ESTree format. It integrates seamlessly with `unist` and `vfile` for processing abstract syntax trees in a structured manner. The current stable version is 2.0.1, with major releases occurring periodically to update Node.js compatibility and internal dependencies. It differentiates itself from raw `acorn` usage by providing a higher-level abstraction suitable for unified processing pipelines, making it ideal for linters, formatters, and other tools that operate on ASTs alongside other types of content. The library primarily focuses on converting serialised JavaScript into its programmatic representation, offering options for specifying JS version, module type, and other parsing rules.","status":"active","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/syntax-tree/esast-util-from-js","tags":["javascript","unist","estree","estree-util","esast","esast-util","util","utility","js","typescript"],"install":[{"cmd":"npm install esast-util-from-js","lang":"bash","label":"npm"},{"cmd":"yarn add esast-util-from-js","lang":"bash","label":"yarn"},{"cmd":"pnpm add esast-util-from-js","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Underlying JavaScript parser for generating the AST.","package":"acorn","optional":false},{"reason":"Provides the ES AST specification (ESTree with additional cleanliness) that this utility outputs.","package":"esast","optional":false},{"reason":"Provides the universal syntax tree specification that esast extends, for broader ecosystem compatibility.","package":"unist","optional":false}],"imports":[{"note":"This package is ESM only since v2.0.0. CommonJS `require` is not supported.","wrong":"const fromJs = require('esast-util-from-js')","symbol":"fromJs","correct":"import { fromJs } from 'esast-util-from-js'"},{"note":"Type import for configuring parsing options, commonly used in TypeScript projects.","symbol":"Options","correct":"import type { Options } from 'esast-util-from-js'"},{"note":"Type import for the input value parameter, typically a string or Uint8Array.","symbol":"Value","correct":"import type { Value } from 'esast-util-from-js'"}],"quickstart":{"code":"import fs from 'node:fs/promises'\nimport { fromJs } from 'esast-util-from-js'\n\nconst exampleCode = `\n  import { someFunction } from './utils.js';\n  const message = 'Hello, esast!';\n  console.log(message, someFunction());\n`;\n\nasync function parseCode() {\n  // In a real application, you might read from a file\n  // const tree = fromJs(await fs.readFile('example.js', 'utf8'), { module: true });\n  const tree = fromJs(exampleCode, { module: true, version: 2023 });\n  console.log('Parsed AST (partial):');\n  console.log(JSON.stringify(tree, null, 2).slice(0, 500) + '...');\n  console.log('\\nRoot type:', tree.type);\n  console.log('Source type:', tree.sourceType);\n  console.log('Number of body nodes:', tree.body.length);\n}\n\nparseCode().catch(console.error);","lang":"typescript","description":"Demonstrates how to parse a JavaScript string into an esast syntax tree, specifying it as a module and a specific ES version, then logs key properties of the resulting AST."},"warnings":[{"fix":"Upgrade your Node.js environment to version 16 or newer. For projects using nvm: `nvm install 16 && nvm use 16`.","message":"Version 2.0.0 of `esast-util-from-js` requires Node.js 16 or higher. Older Node.js versions are no longer supported.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure your project is configured for ESM and use `import` statements. Avoid directly importing from deep paths that are not explicitly exported. Update your `package.json` to include `\"type\": \"module\"` if necessary, or rename your files to `.mjs`.","message":"The package now exclusively uses ES Modules (ESM) and provides an `exports` field in its `package.json`. Direct access to private APIs or CJS `require()` will fail.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"When providing binary input, ensure you use `Uint8Array`, which `Buffer` is a superclass of. Most users passing `string` inputs will not be directly affected, but custom integrations might need updates.","message":"`Buffer` is replaced with `Uint8Array` in the internal API and possibly in types for input `Value` (though `string` is also accepted).","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"To lock down the JavaScript version being parsed, explicitly pass a year (e.g., `{ version: 2023 }`) instead of `'latest'` to ensure consistent parsing behavior.","message":"Using `version: 'latest'` in the options for `fromJs` is a sliding target. It will default to the newest supported ECMAScript year, which can be considered a breaking change across minor versions of `esast-util-from-js`.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement from `const { fromJs } = require('esast-util-from-js')` to `import { fromJs } from 'esast-util-from-js'`. Ensure your project is set up for ES Modules (e.g., `\"type\": \"module\"` in `package.json` or using `.mjs` file extensions).","cause":"Attempting to use `require()` to import `esast-util-from-js` in a CommonJS environment, but the package is ESM only.","error":"ERR_REQUIRE_ESM"},{"fix":"If using modern JavaScript features, try explicitly setting the `version` option to a newer year (e.g., `{ version: 2023 }`). If it's genuinely malformed JavaScript, correct the syntax errors in your input code.","cause":"The input JavaScript contains syntax not supported by the specified (or default) `version` option for `acorn`.","error":"SyntaxError: Unexpected token (at X:Y)"},{"fix":"Inspect the detailed error message within the `VFileMessage` for specifics. This often indicates malformed syntax, missing plugins for non-standard syntax (like JSX), or an incorrect `module` option for the source code type (e.g., parsing ESM as a script).","cause":"The input JavaScript is invalid or cannot be parsed by `acorn` for other reasons, leading to a parsing error wrapped in a `VFileMessage`.","error":"Error: A `VFileMessage` was thrown"}],"ecosystem":"npm"}