xast-util-to-xml
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.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` to import `xast-util-to-xml` in an ESM environment.fixChange `const toXml = require('xast-util-to-xml')` to `import { toXml } from 'xast-util-to-xml'`. -
ERR_PACKAGE_PATH_NOT_EXPORTED
cause Attempting to import a non-exported module path, often a consequence of the `exports` map in `package.json` since v4.0.0.fixEnsure 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'
cause Running a project with xast-util-to-xml v4.0.0 or higher on an unsupported Node.js version (e.g., Node.js 14).fixUpgrade 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.
cause 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`.fixEnsure your `tsconfig.json` targets an appropriate module system (e.g., `"module": "esnext"`). Explicitly import any necessary types, e.g., `import type { Root } from 'xast'`.
Warnings
- breaking Version 4.0.0 of xast-util-to-xml requires Node.js 16 or higher. Ensure your Node.js environment meets this minimum requirement.
- breaking Since version 3.0.0, xast-util-to-xml is an ESM-only package. CommonJS `require()` statements will no longer work.
- breaking Version 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.
- breaking Version 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.
- gotcha The `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.
Install
-
npm install xast-util-to-xml -
yarn add xast-util-to-xml -
pnpm add xast-util-to-xml
Imports
- toXml
const toXml = require('xast-util-to-xml')import { toXml } from 'xast-util-to-xml' - toXml (Deno)
import { toXml } from 'https://esm.sh/xast-util-to-xml@4' - Options
import { Options } from 'xast-util-to-xml'import type { Options } from 'xast-util-to-xml'
Quickstart
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>