Registry / serialization / estree-util-to-js

estree-util-to-js

JSON →
library2.0.0jsnpmunverified

estree-util-to-js is a focused utility designed to transform an ESTree (and ESast) syntax tree into a serialized JavaScript string. Its primary function is to convert in-memory syntax representations back into executable code, often after parsing and transformation within an AST processing pipeline. The current stable version is 2.0.0, which mandates Node.js 16 or higher and functions exclusively as an ESM-only package. While there isn't a fixed release cadence, updates typically occur as needed for features, bug fixes, or to align with evolving JavaScript standards, with major versions reserved for significant breaking changes like environment requirements or API shifts. This package is particularly valuable for projects within the unified ecosystem (unist, vfile) and integrates seamlessly with parsing tools like Acorn. It differentiates itself by providing a robust, extensible mechanism for code generation, including optional source map support and custom handlers for various AST nodes, offering a complementary utility to `esast-util-from-js` which performs the inverse operation.

npm install estree-util-to-js
INSTALL
IMPORT
SIG · ESTREE-UTIL-TO-JS
E
estree-util-to-js
serializationjavascriptv2.0.0
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.

toJs
import { toJs } from 'estree-util-to-js'
const { toJs } = require('estree-util-to-js')
estree-util-to-js is an ESM-only package since v2.0.0. CommonJS `require` will not work.
jsx
import { jsx } from 'estree-util-to-js'
const { jsx } = require('estree-util-to-js')
The `jsx` export provides a map of handlers specifically for JSX nodes.
Map
import type { Map } from 'estree-util-to-js'
This imports the TypeScript type definition for the raw source map object.

Demonstrates how to parse a JavaScript file using Acorn, then serialize its ESTree representation back into JavaScript code using `toJs`, including how to handle the `Result` object.

import fs from 'node:fs/promises'; import { parse } from 'acorn'; import { toJs } from 'estree-util-to-js'; async function serializeExample() { const fileContent = String(await fs.readFile('./input.js', 'utf8')); // Create a dummy file for the example await fs.writeFile('./input.js', 'export const greeting = "Hello, World!";', 'utf8'); const tree = parse(fileContent, { ecmaVersion: 2022, sourceType: 'module', locations: true // Required for source maps, though not used in simple output }); // @ts-expect-error: acorn's types can be complex with estree interop, but it works fine. const result = toJs(tree); console.log('Serialized JavaScript:\n', result.value); // console.log('Source Map (if generated):', result.map); // Clean up the dummy file await fs.unlink('./input.js'); } serializeExample().catch(console.error);
Debug
Known issues
breakingVersion 2.0.0 and above now requires Node.js 16 or newer. Older Node.js versions are not supported.
fix
Upgrade your Node.js environment to version 16 or higher.
affects: >=2.0.0
breakingThe package now strictly uses the `exports` field in its `package.json`. Direct imports to internal files (e.g., `estree-util-to-js/lib/index.js`) or reliance on CommonJS `require` patterns will fail.
fix
Migrate all imports to use standard ESM `import` statements and only import directly from the package name, e.g., `import { toJs } from 'estree-util-to-js'`.
affects: >=2.0.0
gotchaThis package is ESM-only. Attempting to use `require()` or run it in a CommonJS-only environment will result in errors.
fix
Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json` or using `.mjs` files) and use `import` statements for all modules.
affects: >=2.0.0
Errors
Common errors & fixes
require is not defined
Attempting to use CommonJS `require()` syntax to import `estree-util-to-js` in a module that is running as ECMAScript Module (ESM).
fix
Change `const { toJs } = require('estree-util-to-js')` to `import { toJs } from 'estree-util-to-js'`.
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /path/to/node_modules/estree-util-to-js/index.ts
This error can occur if a bundler or environment is misconfigured to try to import TypeScript source files directly instead of compiled JavaScript, especially when `"type": "module"` is set.
fix
Ensure your build tools (e.g., TypeScript compiler, bundler) are correctly configured to resolve `estree-util-to-js` as a JavaScript module (ESM), respecting its `exports` field. Check `tsconfig.json` `moduleResolution` and bundler configurations.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
estree-util-to-js — npm install estree-util-to-js · libregistry