{"id":27860,"library":"hast-util-to-xast","title":"hast-util-to-xast","description":"Utility to transform a hast (HTML) syntax tree into a xast (XML) syntax tree. v3.0.1, ESM-only, fully typed with TypeScript. Supports automatic SVG namespace switching and embedded MDX nodes. Commonly used for EPUB generation. Requires Node.js 16+. Replaced old direct `space` argument with `options.space` in v3.","status":"active","version":"3.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/syntax-tree/hast-util-to-xast","tags":["javascript","dsl","hast-util","hast","html","unist","utility","util","xast-util","typescript"],"install":[{"cmd":"npm install hast-util-to-xast","lang":"bash","label":"npm"},{"cmd":"yarn add hast-util-to-xast","lang":"bash","label":"yarn"},{"cmd":"pnpm add hast-util-to-xast","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Commonly used together to parse HTML before conversion","package":"hast-util-from-html","optional":true},{"reason":"Often used to serialize the resulting xast tree to XML string","package":"xast-util-to-xml","optional":true}],"imports":[{"note":"This package has no default export; use named import only. Also works with `const { toXast } = await import('hast-util-to-xast')`.","wrong":"import toXast from 'hast-util-to-xast'","symbol":"toXast","correct":"import { toXast } from 'hast-util-to-xast'"},{"note":"Options is a TypeScript type, not a runtime value. Use `import type` to avoid runtime inclusion.","wrong":"import { Options } from 'hast-util-to-xast'","symbol":"Options","correct":"import type { Options } from 'hast-util-to-xast'"},{"note":"Space is a TypeScript type, not a runtime value. Use `import type`.","wrong":"import { Space } from 'hast-util-to-xast'","symbol":"Space","correct":"import type { Space } from 'hast-util-to-xast'"}],"quickstart":{"code":"import { fromHtml } from 'hast-util-from-html'\nimport { toXast } from 'hast-util-to-xast'\nimport { toXml } from 'xast-util-to-xml'\n\nconst html = `<!DOCTYPE html><title>Hello</title><h1>World</h1>`\nconst hast = fromHtml(html)\nconst xast = toXast(hast, { space: 'html' })\nconst xml = toXml(xast)\nconsole.log(xml)\n// <?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Hello</title></head><body><h1>World</h1></body></html>","lang":"typescript","description":"Parses HTML with hast-util-from-html, converts to xast, then serializes to XML string."},"warnings":[{"fix":"Pass `{ space: 'svg' }` instead of `'svg'` as second argument.","message":"Removed direct `space` string argument; use `options.space` instead.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Use `import { toXast } from 'hast-util-to-xast'` or dynamic `import()`.","message":"Changed to ESM-only. No CommonJS support starting from v3.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Upgrade Node.js to version 16 or later.","message":"Dropped support for Node.js < 16.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Use explicit `xmlns` attributes for non-SVG custom namespaces (may not be preserved).","message":"When space is 'html', SVG elements are automatically detected and switched to SVG namespace, but custom XML namespaces not supported.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-05-09T00:00:00.000Z","next_check":"2026-08-07T00:00:00.000Z","problems":[{"fix":"Change import to `import { toXast } from 'hast-util-to-xast'`.","cause":"Using default import instead of named import (e.g., `import toXast from 'hast-util-to-xast'`). Package only exports named export.","error":"TypeError: toXast is not a function"},{"fix":"Use `import()` or switch to ESM in your project (package.json type: module).","cause":"Using require() on an ESM-only package.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/hast-util-to-xast/index.js from /path/to/project/index.js not supported."},{"fix":"Change `toXast(hast, 'svg')` to `toXast(hast, { space: 'svg' })`.","cause":"Passing a string as second argument instead of an options object in v3+.","error":"TypeError: The \"original\" argument must be of type function. Received an instance of Object"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}