Registry / serialization / xast-util-to-xml

xast-util-to-xml

JSON →
library4.0.0jsnpmunverified

xast-util-to-xml is a utility within the unified (syntax-tree) ecosystem designed to serialize an xast (XML Abstract Syntax Tree) into a string of XML. The current stable version is 4.0.0, which requires Node.js 16 or higher and is ESM-only. Releases tend to follow a feature-driven cadence, with new major versions introducing breaking changes, particularly around module systems and Node.js compatibility. Key differentiators include its adherence to the unist/xast specification, robust TypeScript type support, and granular options for controlling XML serialization, such as closing empty elements (`<foo/>` vs `<foo></foo>`), preferred attribute quotes, and handling of raw nodes. It is commonly used when converting parsed XML back into a string, either for final output or integration with other tools, offering fine-tuned control over the output format for tasks like minification or pretty-printing.

npm install xast-util-to-xml
INSTALL
IMPORT
SIG · XAST-UTIL-TO-XML
X
xast-util-to-xml
serializationjavascriptv4.0.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.

toXml
import { toXml } from 'xast-util-to-xml'
const toXml = require('xast-util-to-xml')
This package is ESM-only since v3.0.0. Use `import` syntax. Node.js 16+ is required for v4.0.0.
toXml (Deno)
import { toXml } from 'https://esm.sh/xast-util-to-xml@4'
For Deno environments, import directly from esm.sh specifying the major version.
Options
import type { Options } from 'xast-util-to-xml'
import { Options } from 'xast-util-to-xml'
Import the `Options` type using `import type` for type-only imports, especially in TypeScript projects to prevent runtime bundling issues.

This example demonstrates how to create an xast tree using `unist-builder` and `xastscript`, and then serialize it to an XML string using `toXml`, applying custom options.

import { u } from 'unist-builder' import { x } from 'xastscript' import { toXml } from 'xast-util-to-xml' const tree = u('root', [ u('instruction', {name: 'xml'}, 'version="1.0" encoding="utf-8"'), u('text', '\n'), x('ncx', {xmlns: 'http://www.daisy.org/z3986/2005/ncx/', version: '2005-1'}, [ u('text', '\n '), x('head', [ u('text', '\n '), x('meta', {name: 'dtb:uid', content: 'urn:isbn:9781234567891'}), u('text', '\n ') ]), u('text', '\n '), x('docTitle', [x('text', 'A Christmas Carol')]), u('text', '\n '), x('docAuthor', [x('text', 'Charles Dickens')]), u('text', '\n') ]) ]) const xmlOutput = toXml(tree, { closeEmptyElements: true, quote: "'" }) console.log(xmlOutput) // Expected output: // <?xml version='1.0' encoding='utf-8'?> // <ncx xmlns='http://www.daisy.org/z3986/2005/ncx/' version='2005-1'> // <head> // <meta name='dtb:uid' content='urn:isbn:9781234567891'/> // </head> // <docTitle><text>A Christmas Carol</text></docTitle> // <docAuthor><text>Charles Dickens</text></docAuthor> // </ncx>
Debug
Known issues
breakingVersion 4.0.0 of xast-util-to-xml requires Node.js 16 or higher. Ensure your Node.js environment meets this minimum requirement.
fix
Upgrade your Node.js environment to version 16 or newer, or stick to v3.x versions for older Node.js compatibility.
affects: >=4.0.0
breakingSince version 3.0.0, xast-util-to-xml is an ESM-only package. CommonJS `require()` statements will no longer work.
fix
Migrate your codebase to use ES module `import` statements. Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`).
affects: >=3.0.0
breakingVersion 4.0.0 introduced the use of `exports` field in `package.json`. While this generally improves module resolution, it can break applications that relied on direct access to internal, non-exported paths.
fix
Ensure you are only importing publicly exported APIs (e.g., `import { toXml } from 'xast-util-to-xml'`). Avoid deep imports into subdirectories or internal modules.
affects: >=4.0.0
breakingVersion 2.0.0 introduced official TypeScript types. If you previously maintained your own ambient type declarations for `xast-util-to-xml`, these might conflict with the shipped types.
fix
Remove any custom or `@types/xast-util-to-xml` declarations and rely on the types provided by the package itself. Review type errors that may arise due to stricter type checking.
affects: >=2.0.0
gotchaThe `allowDangerousXml` option, when set to `true`, inserts `Raw` nodes directly into the output XML without escaping. This can lead to Cross-Site Scripting (XSS) vulnerabilities if the content of `Raw` nodes is untrusted.
fix
Only enable `allowDangerousXml` if you have complete control over and trust the content of all `Raw` nodes. For user-generated or external content, ensure proper sanitization is applied before creating `Raw` nodes or avoid this option entirely.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use `require()` to import `xast-util-to-xml` in an ESM environment.
fix
Change `const toXml = require('xast-util-to-xml')` to `import { toXml } from 'xast-util-to-xml'`.
ERR_PACKAGE_PATH_NOT_EXPORTED
Attempting to import a non-exported module path, often a consequence of the `exports` map in `package.json` since v4.0.0.
fix
Ensure you are importing `toXml` directly from the package's root: `import { toXml } from 'xast-util-to-xml'` and not from any internal paths like `xast-util-to-xml/lib/index.js`.
Error: Cannot find module 'xast-util-to-xml'
Running a project with xast-util-to-xml v4.0.0 or higher on an unsupported Node.js version (e.g., Node.js 14).
fix
Upgrade your Node.js runtime to version 16 or newer. Alternatively, downgrade `xast-util-to-xml` to a 3.x release.
TS2742: The inferred type of 'toXml' cannot be named without a reference to a UMD global. Consider adding an import of 'xast' or 'unist' to the current file, or migrating to a module-based project.
TypeScript compiler is having trouble resolving types, often due to a mixed CommonJS/ESM setup or missing explicit imports for related type definitions from `xast` or `unist`.
fix
Ensure your `tsconfig.json` targets an appropriate module system (e.g., `"module": "esnext"`). Explicitly import any necessary types, e.g., `import type { Root } from 'xast'`.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
1
Resources
xast-util-to-xml — npm install xast-util-to-xml · libregistry