Registry / serialization / saxen
library11.0.2jsnpmunverified

Saxen is a minimalistic, high-performance SAX-style XML parser designed for JavaScript environments, primarily Node.js. It distinguishes itself by being extremely lightweight (around 2.6KB minified + gzipped) and fast, while offering essential features like optional namespace awareness and entity decoding. The current stable version is 11.0.2, with major versions introducing significant breaking changes, such as the transition to an ESM-only distribution in v11 and stricter Node.js version requirements. Its design prioritizes speed and small footprint, making it suitable for applications where parsing efficiency is critical, and a full DOM parser is unnecessary. Saxen provides a stream of events (`openTag`, `closeTag`, `text`, `error`, etc.) that developers can hook into to process XML data incrementally, and supports an optional "proxy mode" for richer element state at a minor performance cost. Unlike some other parsers, it explicitly eschews certain features to maintain its focus on core SAX parsing.

npm install saxen
INSTALL
IMPORT
SIG · SAXEN
S
saxen
serializationjavascriptv11.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Parser
import { Parser } from 'saxen';
const { Parser } = require('saxen');
Saxen became an ESM-only package in v11. CommonJS `require()` is no longer supported.
Parser (Type Import)
import type { Parser } from 'saxen';
Although saxen is plain JavaScript, if used in a TypeScript project, this is the correct way to import its type for declaration files.

Demonstrates basic usage of the `Parser` class, including event-based parsing, namespace configuration, handling various XML events, and an example of the optional proxy mode.

import { Parser } from 'saxen'; // Instantiate the parser const parser = new Parser(); // Configure namespaces for consistent prefix handling parser.ns({ 'http://foo': 'foo', 'http://bar': 'bar', 'http://baz': 'baz' }); // Register event listeners for different XML parsing stages parser.on('openTag', function(elementName, attrGetter, decodeEntities, selfClosing, getContext) { console.log(`Open Tag: ${elementName}, Self-closing: ${selfClosing}`); const attrs = attrGetter(); console.log('Attributes:', attrs); // Example of using contextGetter const context = getContext(); console.log('Context (line/column):', context.line, context.column); }); parser.on('closeTag', function(elementName, decodeEntities, selfClosing, getContext) { console.log(`Close Tag: ${elementName}`); }); parser.on('text', function(value, decodeEntities, getContext) { if (value.trim().length > 0) { console.log(`Text content: "${value.trim()}"`); } }); parser.on('error', function(err, contextGetter) { console.error('Parsing error:', err.message, 'at', contextGetter().line, contextGetter().column); }); // Parse an example XML string const xmlString = ` <root xmlns="http://foo" xmlns:bar="http://bar" xmlns:baz="http://baz" bar:id="123"> <foo:child attr="value"> Some text content. <baz:nested /> </foo:child> <!-- A comment --> </root> `; console.log('Parsing XML...'); parser.parse(xmlString); console.log('Parsing complete.'); // Demonstrate proxy mode with its own parser instance const proxyParser = new Parser({ proxy: true }); proxyParser.ns({ 'http://proxy-ns': 'pxy' }); proxyParser.on('openTag', function(el, decodeEntities, selfClosing, getContext) { console.log(`[Proxy Mode] Open Tag: ${el.name} (original: ${el.originalName})`); console.log(`[Proxy Mode] Attributes:`, el.attrs); }); proxyParser.parse('<pxy:document xmlns:pxy="http://proxy-ns" version="1.0" />');
Debug
Known issues
breakingSaxen dropped CommonJS (CJS) distribution starting from version 11.0.0. Projects using `require()` will encounter errors.
fix
Migrate your project to use ECMAScript Modules (ESM) with `import { Parser } from 'saxen';` or ensure your Node.js environment supports ESM.
affects: >=11.0.0
breakingThe minimum required Node.js version was updated to `>=20.12` in v11.0.0.
fix
Ensure your Node.js runtime is updated to version 20.12 or higher. Earlier versions will not be supported.
affects: >=11.0.0
gotchaWhen using `proxy` mode, the element object passed to `openTag` and `closeTag` events is a view into the current parser state and changes as parsing advances. It cannot be cached directly.
fix
If you need to retain a persistent copy of the element's state from a specific event, create a shallow clone using `const copy = Object.assign({}, el);`.
affects: >=1.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/saxen/dist/index.mjs from ... not supported.
Attempting to import `saxen` using CommonJS `require()` syntax in an environment that expects ESM, particularly after upgrading to saxen v11+.
fix
Update your import statement to `import { Parser } from 'saxen';` and ensure your project is configured for ESM (e.g., by adding `"type": "module"` to your `package.json` or using `.mjs` file extensions).
TypeError: Parser is not a constructor
Incorrect import syntax for the `Parser` class, or attempting to use `new Parser()` after an incorrect `require()` in an ESM context.
fix
Ensure you are using the correct named import for ESM: `import { Parser } from 'saxen';`.
SyntaxError: Cannot use import statement outside a module
Using `import` statements in a JavaScript file that is being treated as a CommonJS module (e.g., a `.js` file without `"type": "module"` in `package.json` in Node.js).
fix
Set `"type": "module"` in your `package.json` or rename your file to use the `.mjs` extension. Alternatively, you might need to transpile your code if targeting older Node.js versions or browser environments without native ESM support.
Upgrade
Version history
11.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
OpenAI (training)
1
Resources
saxen — npm install saxen · libregistry