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-formatterNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Demonstrates basic pretty-printing and minifying of an XML string with options for indentation, collapse, filter, ignoredPaths, and line separator.
Switch to import xmlFormat from 'xml-formatter'. If you need CommonJS, use dynamic import or downgrade to v2.x.
Explicitly set lineSeparator: '\n' if you need Unix-style line endings.
Rename the option to 'spaceBeforeSlashInSelfClosingTag'.
Handle errors with try/catch when throwOnFailure is true (default), or set throwOnFailure: false to suppress errors.
Test your filter function on sample XML to verify it doesn't remove necessary structure.
Add 'type': 'module' to your package.json, or rename file to .mjs, or use dynamic import: const xmlFormat = (await import('xml-formatter')).default;Use default import: import xmlFormat from 'xml-formatter'.
Use import statement for ESM, or in browser use the script tag that defines a global or CommonJS require (dist/browser/xml-formatter.js).
No dependency data recorded yet.