{"id":12684,"library":"xast-util-to-xml","title":"xast-util-to-xml","description":"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.","status":"active","version":"4.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/syntax-tree/xast-util-to-xml","tags":["javascript","unist","xast","xast-util","util","utility","xml","typescript"],"install":[{"cmd":"npm install xast-util-to-xml","lang":"bash","label":"npm"},{"cmd":"yarn add xast-util-to-xml","lang":"bash","label":"yarn"},{"cmd":"pnpm add xast-util-to-xml","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is ESM-only since v3.0.0. Use `import` syntax. Node.js 16+ is required for v4.0.0.","wrong":"const toXml = require('xast-util-to-xml')","symbol":"toXml","correct":"import { toXml } from 'xast-util-to-xml'"},{"note":"For Deno environments, import directly from esm.sh specifying the major version.","symbol":"toXml (Deno)","correct":"import { toXml } from 'https://esm.sh/xast-util-to-xml@4'"},{"note":"Import the `Options` type using `import type` for type-only imports, especially in TypeScript projects to prevent runtime bundling issues.","wrong":"import { Options } from 'xast-util-to-xml'","symbol":"Options","correct":"import type { Options } from 'xast-util-to-xml'"}],"quickstart":{"code":"import { u } from 'unist-builder'\nimport { x } from 'xastscript'\nimport { toXml } from 'xast-util-to-xml'\n\nconst tree = u('root', [\n  u('instruction', {name: 'xml'}, 'version=\"1.0\" encoding=\"utf-8\"'),\n  u('text', '\\n'),\n  x('ncx', {xmlns: 'http://www.daisy.org/z3986/2005/ncx/', version: '2005-1'}, [\n    u('text', '\\n  '),\n    x('head', [\n      u('text', '\\n    '),\n      x('meta', {name: 'dtb:uid', content: 'urn:isbn:9781234567891'}),\n      u('text', '\\n  ')\n    ]),\n    u('text', '\\n  '),\n    x('docTitle', [x('text', 'A Christmas Carol')]),\n    u('text', '\\n  '),\n    x('docAuthor', [x('text', 'Charles Dickens')]),\n    u('text', '\\n')\n  ])\n])\n\nconst xmlOutput = toXml(tree, { closeEmptyElements: true, quote: \"'\" })\nconsole.log(xmlOutput)\n// Expected output:\n// <?xml version='1.0' encoding='utf-8'?>\n// <ncx xmlns='http://www.daisy.org/z3986/2005/ncx/' version='2005-1'>\n//   <head>\n//     <meta name='dtb:uid' content='urn:isbn:9781234567891'/>\n//   </head>\n//   <docTitle><text>A Christmas Carol</text></docTitle>\n//   <docAuthor><text>Charles Dickens</text></docAuthor>\n// </ncx>","lang":"typescript","description":"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."},"warnings":[{"fix":"Upgrade your Node.js environment to version 16 or newer, or stick to v3.x versions for older Node.js compatibility.","message":"Version 4.0.0 of xast-util-to-xml requires Node.js 16 or higher. Ensure your Node.js environment meets this minimum requirement.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Migrate your codebase to use ES module `import` statements. Ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json`).","message":"Since version 3.0.0, xast-util-to-xml is an ESM-only package. CommonJS `require()` statements will no longer work.","severity":"breaking","affected_versions":">=3.0.0"},{"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.","message":"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.","severity":"breaking","affected_versions":">=4.0.0"},{"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.","message":"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.","severity":"breaking","affected_versions":">=2.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change `const toXml = require('xast-util-to-xml')` to `import { toXml } from 'xast-util-to-xml'`.","cause":"Attempting to use `require()` to import `xast-util-to-xml` in an ESM environment.","error":"ReferenceError: require is not defined"},{"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`.","cause":"Attempting to import a non-exported module path, often a consequence of the `exports` map in `package.json` since v4.0.0.","error":"ERR_PACKAGE_PATH_NOT_EXPORTED"},{"fix":"Upgrade your Node.js runtime to version 16 or newer. Alternatively, downgrade `xast-util-to-xml` to a 3.x release.","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).","error":"Error: Cannot find module 'xast-util-to-xml'"},{"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'`.","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`.","error":"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."}],"ecosystem":"npm"}