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.
npm install astringVerified import paths — ran on the pinned version, not inferred.
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.
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';`
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.
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.
Use correct named import syntax: `import { generate } from 'astring';` for ESM or `const { generate } = require('astring');` for CommonJS.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.
No dependency data recorded yet.