Registry / devops / xml-formatter

xml-formatter

JSON →
library3.7.0jsnpmunverified

xml-formatter is a lightweight library (v3.7.0) that converts XML strings into human-readable, pretty-printed format while respecting the xml:space attribute. It also supports minifying formatted XML back to compact strings. The library is zero-dependency, ships TypeScript definitions, and includes a browser bundle. It stands out by preserving xml:space, offering fine-grained formatting options (indentation, line separators, collapse content, filter, ignored paths), and throwing on parse failure by default. Release cadence is moderate; actively maintained. The package bundles for both Node (ESM) and browser (CJS/global). Differentiators: respects xml:space attribute, provides filter and ignoredPaths for selective formatting, and includes minify mode.

npm install xml-formatter
INSTALL
IMPORT
SIG · XML-FORMATTER
X
xml-formatter
devopsjavascriptv3.7.0
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.

default
import xmlFormat from 'xml-formatter'
const xmlFormat = require('xml-formatter')
Package is ESM-only since v3.0.0 when Node >= 14. In v3.7.0, import default is the standard way. CommonJS require works only in browser via the bundled dist/browser/xml-formatter.js script, where it sets module.exports or creates a global. For Node, you must use ESM import.
minify
import xmlFormat, { minify } from 'xml-formatter'
minify is a named export attached to the default export as xmlFormat.minify(). Both forms work: import { minify } or use xmlFormat.minify().
xmlFormat
import xmlFormat from 'xml-formatter'
import { xmlFormat } from 'xml-formatter'
Common mistake: trying named import for default export. xmlFormat is the default export, not a named one. The package does not export a named function xmlFormat; the default export is the formatter function.
XmlFormatterOptions
import type { XmlFormatterOptions } from 'xml-formatter'
TypeScript users can import the options type for type safety. Requires TypeScript 4.5+ with verbatimModuleSyntax. Named export available.

Demonstrates basic pretty-printing and minifying of an XML string with options for indentation, collapse, filter, ignoredPaths, and line separator.

import xmlFormat from 'xml-formatter'; const input = '<root><content><p xml:space="preserve">This is <b>some</b> content.</p></content></root>'; const formatted = xmlFormat(input, { indentation: ' ', collapseContent: true, lineSeparator: '\n', filter: (node: any) => node.type !== 'Comment', ignoredPaths: ['/root/content'], }); console.log(formatted); // Output: // <root> // <content> // <p xml:space="preserve">This is <b>some</b> content.</p> // </content> // </root> // Minify example import { minify } from 'xml-formatter'; const minified = minify(formatted); console.log(minified); // Output: <root><content><p xml:space="preserve">This is <b>some</b> content.</p></content></root>
Debug
Known issues
breakingBreaking change in v3: Package is now ESM-only. Code that uses require('xml-formatter') in Node.js will fail.
fix
Switch to import xmlFormat from 'xml-formatter'. If you need CommonJS, use dynamic import or downgrade to v2.x.
affects: >=3.0.0 <4.0.0
breakingBreaking change in v2.3.0: Default lineSeparator changed from '\n' to '\r\n' (Windows-style).
fix
Explicitly set lineSeparator: '\n' if you need Unix-style line endings.
affects: >=2.3.0
deprecatedOption 'whiteSpaceAtEndOfSelfclosingTag' is deprecated since v3.0.0. Use 'spaceBeforeSlashInSelfClosingTag' instead.
fix
Rename the option to 'spaceBeforeSlashInSelfClosingTag'.
affects: >=3.0.0
gotchaWhen using throwOnFailure: true (default), invalid XML will throw an error. If you set throwOnFailure: false, the original string is returned on parse failure.
fix
Handle errors with try/catch when throwOnFailure is true (default), or set throwOnFailure: false to suppress errors.
affects: >=1.0.0
gotchaThe filter function receives a node object with properties: type, name, attributes, children, content. If you return false for a node, it will be omitted from output. Be careful not to filter out essential structural nodes.
fix
Test your filter function on sample XML to verify it doesn't remove necessary structure.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Trying to use ESM import syntax in a CommonJS Node.js project or without 'type': 'module' in package.json.
fix
Add 'type': 'module' to your package.json, or rename file to .mjs, or use dynamic import: const xmlFormat = (await import('xml-formatter')).default;
TypeError: xmlFormat is not a function
Named import instead of default import: import { xmlFormat } from 'xml-formatter'.
fix
Use default import: import xmlFormat from 'xml-formatter'.
Uncaught ReferenceError: require is not defined
Trying to use require in an ESM environment (Node ESM or browser with ES modules).
fix
Use import statement for ESM, or in browser use the script tag that defines a global or CommonJS require (dist/browser/xml-formatter.js).
Upgrade
Version history
3.7.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Amazon
1
Resources
xml-formatter — npm install xml-formatter · libregistry