Registry / serialization / xastscript

xastscript

JSON →
library4.0.0jsnpmunverified

xastscript is a utility library providing a hyperscript-like interface for programmatically creating xast (XML Abstract Syntax Tree) nodes. Similar to React's `createElement` or `hastscript` for HTML, it simplifies the creation of XML syntax trees by allowing nested function calls instead of verbose object literals. The current stable version is 4.0.0, which requires Node.js 16 or later and is exclusively ESM. It's part of the unified ecosystem and is primarily used when generating XML structures within code. Unlike `unist-builder` which handles generic unist nodes, `xastscript` is specifically tailored for XML elements, providing convenience for defining attributes and children in a more declarative way. Its release cadence typically involves minor and patch updates every few months, with major versions occurring less frequently, driven by breaking changes or significant feature additions.

npm install xastscript
INSTALL
IMPORT
SIG · XASTSCRIPT
X
xastscript
serializationjavascriptv4.0.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

x
✓ import { x } from 'xastscript'
✗ const { x } = require('xastscript')
xastscript is ESM-only since v3.0.0. For Node.js, ensure your environment supports ES Modules or use a dynamic import. v4.0.0 requires Node.js 16+.
Attributes
✓ import type { Attributes } from 'xastscript'
✗ import { Attributes } from 'xastscript'
The `Attributes` type defines the expected shape of properties passed to the `x` function. Import it as a type.
Result
✓ import type { Result } from 'xastscript'
✗ import { Result } from 'xastscript'
The `Result` type represents the possible return types of the `x` function (i.e., xast nodes). Import it as a type.

Demonstrates creating xast XML elements with attributes and children using both array and argument-based child passing, and integrating with `unist-builder` for non-element xast nodes like comments and processing instructions.

import { u } from 'unist-builder'; import { x } from 'xastscript'; // Children as an array: console.log( x('album', {id: 123}, [ x('name', 'Born in the U.S.A.'), x('artist', 'Bruce Springsteen'), x('releasedate', '1984-04-06') ]) ); // Children as arguments: console.log( x( 'album', {id: 456}, x('name', 'Exile in Guyville'), x('artist', 'Liz Phair'), x('releasedate', '1993-06-22') ) ); // Combining with unist-builder for other xast nodes (comments, instructions, cdata): console.log( x(null, [ u('instruction', {name: 'xml'}, 'version="1.0" encoding="UTF-8"'), x('album', [ u('comment', 'A fantastic rock album!'), x('name', 'Born in the U.S.A.'), x('description', [u('cdata', '3 < 5 & 8 > 13')]) ]) ]) );
Debug
Known issues
breakingxastscript v4.0.0 requires Node.js 16 or higher. Older Node.js versions are no longer supported.
fix
Upgrade your Node.js environment to version 16 or later. If unable to upgrade, consider using `xastscript` v3.x which supports Node.js 12+.
affects: >=4.0.0
breakingxastscript transitioned to an ESM-only package starting with v3.0.0. Direct CommonJS `require()` calls are no longer supported.
fix
Refactor your imports to use ES Modules syntax (`import { x } from 'xastscript';`). Ensure your project is configured for ESM by setting `"type": "module"` in your `package.json` or by using `.mjs` file extensions. In CommonJS files, dynamic `import()` can be used.
affects: >=3.0.0
breakingTypeScript types are now shipped directly with the package since v2.0.0. If you were using `@types/xastscript`, you should remove it. V4.0.0 specifically updated `@types/xast`, potentially requiring updates in related type definitions.
fix
Remove `@types/xastscript` from your `devDependencies`. Ensure your `tsconfig.json` correctly resolves types from `xastscript` itself. Review and update any explicit dependencies on `@types/xast`.
affects: >=2.0.0
gotchaThe `x` function can serve as a JSX pragma, allowing XML-like syntax directly in your code. This requires specific configuration in your TypeScript or Babel setup.
fix
To use JSX with `xastscript`, configure your `tsconfig.json` (for TypeScript) or Babel presets. For example, in `tsconfig.json`: `"compilerOptions": { "jsx": "react-jsx", "jsxFactory": "x", "jsxFragmentFactory": null }`.
affects: >=2.1.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Attempting to use `require()` to import `xastscript`, which is an ES Module.
fix
Change `const { x } = require('xastscript');` to `import { x } from 'xastscript';`. Ensure your `package.json` includes `"type": "module"` if this is a Node.js project or use `.mjs` file extensions.
TypeError: xastscript_1.x is not a function
This error typically occurs when a CommonJS consumer attempts to use an ESM package that has been incorrectly transpiled or bundled, or when trying to default import a named export.
fix
Verify your build setup handles ES Modules correctly. Always use named imports for `x` (e.g., `import { x } from 'xastscript';`). If using TypeScript, check `tsconfig.json` for `moduleResolution` and `module` settings.
TypeError: Cannot read properties of undefined (reading 'x')
This can happen if the `xastscript` module isn't resolved correctly, or if a global `x` is expected when it's not available, often due to a failed or incorrect import.
fix
Double-check your `import { x } from 'xastscript';` statement for typos. Ensure `xastscript` is correctly installed in `node_modules`. If running in a browser, ensure `esm.sh` or similar CDN imports are correct and bundled if necessary.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
18
OpenAI (training)
1
Resources
xastscript — npm install xastscript · libregistry