Registry / serialization / astring

astring

JSON →
library1.9.0jsnpmunverified

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 astring
INSTALL
IMPORT
SIG · ASTRING
A
astring
serializationjavascriptv1.9.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.

generate
import { generate } from 'astring';
const generate = require('astring');
Astring primarily uses named exports. For CommonJS, use `const { generate } = require('astring');` for named destructuring.
GENERATOR
import { GENERATOR } from 'astring';
const { GENERATOR } = require('astring');
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.
astring (global)
// In HTML: <script src="astring.min.js"></script> // In JS: var generate = astring.generate;
import { generate } from './dist/astring.min.js';
The `dist/astring.min.js` bundle exposes `astring` as a global variable in browser environments, not as an ESM module for direct import.

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.

import { generate } from 'astring'; import * as acorn from 'acorn'; // npm install acorn const sourceCode = ` const add = (a, b) => { // This is a simple addition function return a + b; }; function subtract(x, y) { return x - y; } const result = add(process.env.NUM_ONE ?? '5', process.env.NUM_TWO ?? '3'); console.log('Result:', result); `; // Parse the source code into an ESTree-compliant AST const ast = acorn.parse(sourceCode, { ecmaVersion: 2024, // Use a recent ECMAScript version sourceType: 'module', }); // Generate code from the AST with specific options const generatedCode = generate(ast, { indent: ' ', // Use 2 spaces for indentation lineEnd: '\n', // Use newline for line endings comments: true, // Enable comment generation }); console.log('--- Original Source ---\n' + sourceCode); console.log('\n--- Generated Code ---\n' + generatedCode);
Debug
Known issues
gotchaAstring 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.
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';`
affects: >=1.0.0
gotchaAstring 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.
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.
affects: >=1.0.0
gotchaEnabling 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.
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: generate is not a function
Attempting to use `require('astring')` or `import generate from 'astring'` when `generate` is a named export.
fix
Use correct named import syntax: `import { generate } from 'astring';` for ESM or `const { generate } = require('astring');` for CommonJS.
TypeError: Cannot read properties of undefined (reading 'type')
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).
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.
Upgrade
Version history
1.9.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources