Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
toXast
✓ import { toXast } from 'hast-util-to-xast'
✗ import toXast from 'hast-util-to-xast'
This package has no default export; use named import only. Also works with `const { toXast } = await import('hast-util-to-xast')`.
Options
✓ import type { Options } from 'hast-util-to-xast'
✗ import { Options } from 'hast-util-to-xast'
Options is a TypeScript type, not a runtime value. Use `import type` to avoid runtime inclusion.
Space
✓ import type { Space } from 'hast-util-to-xast'
✗ import { Space } from 'hast-util-to-xast'
Space is a TypeScript type, not a runtime value. Use `import type`.
Parses HTML with hast-util-from-html, converts to xast, then serializes to XML string.
import { fromHtml } from 'hast-util-from-html'
import { toXast } from 'hast-util-to-xast'
import { toXml } from 'xast-util-to-xml'
const html = `<!DOCTYPE html><title>Hello</title><h1>World</h1>`
const hast = fromHtml(html)
const xast = toXast(hast, { space: 'html' })
const xml = toXml(xast)
console.log(xml)
// <?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>
Errors
Common errors & fixes
TypeError: toXast is not a function
Using default import instead of named import (e.g., `import toXast from 'hast-util-to-xast'`). Package only exports named export.
fixChange import to `import { toXast } from 'hast-util-to-xast'`. Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/hast-util-to-xast/index.js from /path/to/project/index.js not supported.
Using require() on an ESM-only package.
fixUse `import()` or switch to ESM in your project (package.json type: module).
TypeError: The "original" argument must be of type function. Received an instance of Object
Passing a string as second argument instead of an options object in v3+.
fixChange `toXast(hast, 'svg')` to `toXast(hast, { space: 'svg' })`. Audit
Dependencies
hast-util-from-htmloptionalCommonly used together to parse HTML before conversion
xast-util-to-xmloptionalOften used to serialize the resulting xast tree to XML string