{"id":10523,"library":"astring","title":"Astring: Fast ESTree Code Generator","description":"Astring is a lightweight and high-performance JavaScript code generator that transforms an ESTree-compliant Abstract Syntax Tree (AST) back into JavaScript source code. Currently at version 1.9.0, the library sees frequent minor and patch releases, indicating active development and responsiveness to new language features and bug fixes. Its key differentiators include exceptional speed (outperforming alternatives like Babel, Escodegen, and Prettier by significant margins), a tiny footprint (approx. 4 KB gzipped), and zero runtime dependencies. Astring supports JavaScript up to ES2024 and stage 3 proposals, can be extended with custom AST node handlers, and integrates with source map and comment generation tools. It's compatible with ASTs produced by parsers such as Acorn and Meriyah, and runs in Node.js, browsers, and Deno.","status":"active","version":"1.9.0","language":"javascript","source_language":"en","source_url":"https://github.com/davidbonnet/astring","tags":["javascript","ast","codegen","code generator","estree","astravel","typescript"],"install":[{"cmd":"npm install astring","lang":"bash","label":"npm"},{"cmd":"yarn add astring","lang":"bash","label":"yarn"},{"cmd":"pnpm add astring","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Astring primarily uses named exports. For CommonJS, use `const { generate } = require('astring');` for named destructuring.","wrong":"const generate = require('astring');","symbol":"generate","correct":"import { generate } from 'astring';"},{"note":"The base generator object, useful for extending Astring's code generation logic with custom handlers for specific AST node types. The CommonJS equivalent `require` also uses named destructuring.","wrong":"const { GENERATOR } = require('astring');","symbol":"GENERATOR","correct":"import { GENERATOR } from 'astring';"},{"note":"The `dist/astring.min.js` bundle exposes `astring` as a global variable in browser environments, not as an ESM module for direct import.","wrong":"import { generate } from './dist/astring.min.js';","symbol":"astring (global)","correct":"// In HTML:\n<script src=\"astring.min.js\"></script>\n// In JS:\nvar generate = astring.generate;"}],"quickstart":{"code":"import { generate } from 'astring';\nimport * as acorn from 'acorn'; // npm install acorn\n\nconst sourceCode = `\nconst add = (a, b) => {\n  // This is a simple addition function\n  return a + b;\n};\n\nfunction subtract(x, y) {\n  return x - y;\n}\n\nconst result = add(process.env.NUM_ONE ?? '5', process.env.NUM_TWO ?? '3');\nconsole.log('Result:', result);\n`;\n\n// Parse the source code into an ESTree-compliant AST\nconst ast = acorn.parse(sourceCode, {\n  ecmaVersion: 2024, // Use a recent ECMAScript version\n  sourceType: 'module',\n});\n\n// Generate code from the AST with specific options\nconst generatedCode = generate(ast, {\n  indent: '  ', // Use 2 spaces for indentation\n  lineEnd: '\\n', // Use newline for line endings\n  comments: true, // Enable comment generation\n});\n\nconsole.log('--- Original Source ---\\n' + sourceCode);\nconsole.log('\\n--- Generated Code ---\\n' + generatedCode);\n","lang":"typescript","description":"This quickstart demonstrates parsing a JavaScript string into an ESTree AST using Acorn, then generating formatted code from that AST using Astring, including comments and custom indentation. It also shows using environment variables for dynamic values."},"warnings":[{"fix":"For older environments, install and import polyfills like `npm install string.prototype.repeat string.prototype.endsWith` and `import 'string.prototype.repeat'; import 'string.prototype.endsWith';`","message":"Astring relies on `String.prototype.repeat` and `String.prototype.endsWith`. If targeting older JavaScript environments (e.g., IE) that lack these methods, you must include appropriate polyfills (e.g., `string.prototype.repeat`, `string.prototype.endsWith`, or `babel-polyfill`) for Astring to function correctly.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your AST is generated by a reputable, ESTree-compliant parser (e.g., Acorn, Meriyah, Espree) and matches the expected structure for Astring's handlers.","message":"Astring expects an ESTree-compliant AST. Feeding it a malformed or non-compliant AST, or one generated by a parser with significant deviations from the ESTree spec, may result in incorrect code generation or runtime errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Only enable `comments: true` or use custom generators when necessary. For maximum performance in production, disable comment generation if comments are not required in the output.","message":"Enabling comment generation (`comments: true`) or providing complex custom generators can impact performance. While Astring is designed for speed, these options add overhead and should be considered carefully in performance-critical applications.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use correct named import syntax: `import { generate } from 'astring';` for ESM or `const { generate } = require('astring');` for CommonJS.","cause":"Attempting to use `require('astring')` or `import generate from 'astring'` when `generate` is a named export.","error":"TypeError: generate is not a function"},{"fix":"Inspect the input AST to ensure it's a valid ESTree-compliant object and that all its nodes have a `type` property. This often happens if the parsing failed or a transformation corrupted the AST.","cause":"The AST node being processed by `astring.generate` is either `undefined`, `null`, or does not conform to the expected ESTree structure (e.g., missing a `type` property).","error":"TypeError: Cannot read properties of undefined (reading 'type')"}],"ecosystem":"npm"}