Registry / serialization / esast-util-from-js

esast-util-from-js

JSON →
library2.0.1jsnpmunverified

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.

npm install esast-util-from-js
INSTALL
IMPORT
SIG · ESAST-UTIL-FROM-JS
E
esast-util-from-js
serializationjavascriptv2.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fromJs
import { fromJs } from 'esast-util-from-js'
const fromJs = require('esast-util-from-js')
This package is ESM only since v2.0.0. CommonJS `require` is not supported.
Options
import type { Options } from 'esast-util-from-js'
Type import for configuring parsing options, commonly used in TypeScript projects.
Value
import type { Value } from 'esast-util-from-js'
Type import for the input value parameter, typically a string or Uint8Array.

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.

import fs from 'node:fs/promises' import { fromJs } from 'esast-util-from-js' const exampleCode = ` import { someFunction } from './utils.js'; const message = 'Hello, esast!'; console.log(message, someFunction()); `; async function parseCode() { // In a real application, you might read from a file // const tree = fromJs(await fs.readFile('example.js', 'utf8'), { module: true }); const tree = fromJs(exampleCode, { module: true, version: 2023 }); console.log('Parsed AST (partial):'); console.log(JSON.stringify(tree, null, 2).slice(0, 500) + '...'); console.log('\nRoot type:', tree.type); console.log('Source type:', tree.sourceType); console.log('Number of body nodes:', tree.body.length); } parseCode().catch(console.error);
Debug
Known issues
breakingVersion 2.0.0 of `esast-util-from-js` requires Node.js 16 or higher. Older Node.js versions are no longer supported.
fix
Upgrade your Node.js environment to version 16 or newer. For projects using nvm: `nvm install 16 && nvm use 16`.
affects: >=2.0.0
breakingThe 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.
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`.
affects: >=2.0.0
breaking`Buffer` is replaced with `Uint8Array` in the internal API and possibly in types for input `Value` (though `string` is also accepted).
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.
affects: >=2.0.0
gotchaUsing `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`.
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.
affects: *
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Attempting to use `require()` to import `esast-util-from-js` in a CommonJS environment, but the package is ESM only.
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).
SyntaxError: Unexpected token (at X:Y)
The input JavaScript contains syntax not supported by the specified (or default) `version` option for `acorn`.
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.
Error: A `VFileMessage` was thrown
The input JavaScript is invalid or cannot be parsed by `acorn` for other reasons, leading to a parsing error wrapped in a `VFileMessage`.
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).
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
acornrequiredUnderlying JavaScript parser for generating the AST.
esastrequiredProvides the ES AST specification (ESTree with additional cleanliness) that this utility outputs.
unistrequiredProvides the universal syntax tree specification that esast extends, for broader ecosystem compatibility.
Agent activity
2 hits · last 30 days
node
2
Resources
esast-util-from-js — npm install esast-util-from-js · libregistry