{"id":12729,"library":"estree-util-to-js","title":"Serialize ESTree to JavaScript","description":"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.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/syntax-tree/estree-util-to-js","tags":["javascript","unist","estree","estree-util","esast","esast-util","util","utility","js","typescript"],"install":[{"cmd":"npm install estree-util-to-js","lang":"bash","label":"npm"},{"cmd":"yarn add estree-util-to-js","lang":"bash","label":"yarn"},{"cmd":"pnpm add estree-util-to-js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"estree-util-to-js is an ESM-only package since v2.0.0. CommonJS `require` will not work.","wrong":"const { toJs } = require('estree-util-to-js')","symbol":"toJs","correct":"import { toJs } from 'estree-util-to-js'"},{"note":"The `jsx` export provides a map of handlers specifically for JSX nodes.","wrong":"const { jsx } = require('estree-util-to-js')","symbol":"jsx","correct":"import { jsx } from 'estree-util-to-js'"},{"note":"This imports the TypeScript type definition for the raw source map object.","symbol":"Map","correct":"import type { Map } from 'estree-util-to-js'"}],"quickstart":{"code":"import fs from 'node:fs/promises';\nimport { parse } from 'acorn';\nimport { toJs } from 'estree-util-to-js';\n\nasync function serializeExample() {\n  const fileContent = String(await fs.readFile('./input.js', 'utf8'));\n\n  // Create a dummy file for the example\n  await fs.writeFile('./input.js', 'export const greeting = \"Hello, World!\";', 'utf8');\n\n  const tree = parse(fileContent, {\n    ecmaVersion: 2022,\n    sourceType: 'module',\n    locations: true // Required for source maps, though not used in simple output\n  });\n\n  // @ts-expect-error: acorn's types can be complex with estree interop, but it works fine.\n  const result = toJs(tree);\n\n  console.log('Serialized JavaScript:\\n', result.value);\n  // console.log('Source Map (if generated):', result.map);\n\n  // Clean up the dummy file\n  await fs.unlink('./input.js');\n}\n\nserializeExample().catch(console.error);","lang":"typescript","description":"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."},"warnings":[{"fix":"Upgrade your Node.js environment to version 16 or higher.","message":"Version 2.0.0 and above now requires Node.js 16 or newer. Older Node.js versions are not supported.","severity":"breaking","affected_versions":">=2.0.0"},{"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'`.","message":"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.","severity":"breaking","affected_versions":">=2.0.0"},{"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.","message":"This package is ESM-only. Attempting to use `require()` or run it in a CommonJS-only environment will result in errors.","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":"Change `const { toJs } = require('estree-util-to-js')` to `import { toJs } from 'estree-util-to-js'`.","cause":"Attempting to use CommonJS `require()` syntax to import `estree-util-to-js` in a module that is running as ECMAScript Module (ESM).","error":"require is not defined"},{"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.","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.","error":"TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension \".ts\" for /path/to/node_modules/estree-util-to-js/index.ts"}],"ecosystem":"npm"}