Registry / devops / xml-beautify

xml-beautify

JSON →
library1.2.3jsnpmunverified

xml-beautify is an XML pretty-printer for Node.js and browser, version 1.2.3, released under MIT license. It formats XML text with customizable indent and self-closing element options. Maintained by riversun, it provides a simple API and works in both environments. Unlike alternatives, it preserves CDATA sections and self-closing tags. The package requires an external DOMParser (e.g., xmldom) when used in Node.js. The latest version is stable with no recent updates.

npm install xml-beautify
INSTALL
IMPORT
SIG · XML-BEAUTIFY
X
xml-beautify
devopsjavascriptv1.2.3
harness data pending
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.

XmlBeautify
import XmlBeautify from 'xml-beautify'
const { XmlBeautify } = require('xml-beautify')
Package exports default export only, no named export.
XmlBeautify
const XmlBeautify = require('xml-beautify')
const { XmlBeautify } = require('xml-beautify')
CommonJS require returns the XmlBeautify constructor directly.
DOMParser
const { DOMParser } = require('xmldom')
const DOMParser = require('xmldom')
xmldom exports DOMParser as a named export, not default.

Shows how to use xml-beautify in Node.js with xmldom DOMParser, including indent and self-closing options.

const XmlBeautify = require('xml-beautify'); const { DOMParser } = require('xmldom'); const srcXml = `<?xml version="1.0" encoding="utf-8"?><root><child>text</child></root>`; const beautifier = new XmlBeautify({ parser: DOMParser }); const beautified = beautifier.beautify(srcXml, { indent: ' ', useSelfClosingElement: true }); console.log(beautified);
Debug
Known issues
gotchaNode.js requires an external DOMParser like xmldom; browser has built-in DOMParser.
fix
Install xmldom: npm install xmldom, then pass { parser: DOMParser } to constructor.
affects: >=1.0.0
gotchaThe package uses default export; named import { XmlBeautify } will be undefined.
fix
Use default import: import XmlBeautify from 'xml-beautify' or const XmlBeautify = require('xml-beautify').
affects: >=1.0.0
deprecatedThe option 'useSelfClosingElement' defaults to true in latest versions; setting to false may produce unexpected output.
fix
Explicitly set useSelfClosingElement: false if you want expanded empty elements.
affects: >=1.2.0
gotchaWhen using CDN, script tag loads 'XmlBeautify.js' which sets global XmlBeautify; ensure no module loader conflict.
fix
Use script src='https://cdn.jsdelivr.net/npm/xml-beautify@1.2.3/dist/XmlBeautify.js' and access via window.XmlBeautify.
affects: >=1.2.0
Errors
Common errors & fixes
Error: DOMParser is not defined
Running in Node.js without providing a DOMParser or installing xmldom.
fix
Install xmldom: npm install xmldom, then require it and pass to constructor: new XmlBeautify({ parser: DOMParser }).
TypeError: Cannot read properties of undefined (reading 'beautify')
Attempted to destructure XmlBeautify as named import when it is default export.
fix
Use correct import: const XmlBeautify = require('xml-beautify'); or import XmlBeautify from 'xml-beautify';
ReferenceError: XmlBeautify is not defined
CDN script not loaded before usage, or using ES module import without type='module'.
fix
Ensure script tag loads XmlBeautify.js before calling new XmlBeautify(), or add type='module' for ES modules.
Upgrade
Version history
1.2.3latest on npm
Audit
Dependencies
xmldomoptionalRequired when running in Node.js for XML parsing; not needed in browser.
Agent activity
12 hits · last 30 days
node
8
Amazon
1
Bingbot
1
Resources
xml-beautify — npm install xml-beautify · libregistry