Serialize ESTree to JavaScript
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.
Common errors
-
require is not defined
cause Attempting to use CommonJS `require()` syntax to import `estree-util-to-js` in a module that is running as ECMAScript Module (ESM).fixChange `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
cause 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.fixEnsure 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.
Warnings
- breaking Version 2.0.0 and above now requires Node.js 16 or newer. Older Node.js versions are not supported.
- breaking The 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.
- gotcha This package is ESM-only. Attempting to use `require()` or run it in a CommonJS-only environment will result in errors.
Install
-
npm install estree-util-to-js -
yarn add estree-util-to-js -
pnpm add estree-util-to-js
Imports
- toJs
const { toJs } = require('estree-util-to-js')import { toJs } from 'estree-util-to-js' - jsx
const { jsx } = require('estree-util-to-js')import { jsx } from 'estree-util-to-js' - Map
import type { Map } from 'estree-util-to-js'
Quickstart
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);