Registry / serialization / parse5

parse5

JSON →
library8.0.1jsnpmunverified

parse5 is a spec-compliant HTML parsing and serialization library for JavaScript, faithfully implementing the HTML5 specification (WHATWG). It offers robust parsing capabilities, creating a syntax tree that can then be manipulated or serialized back into HTML. The current stable version is 8.0.1, with releases occurring periodically to address dependencies, bug fixes, and minor enhancements. Major version bumps (like v7.0.0 and v8.0.0) typically introduce significant internal refactors or breaking changes. Key differentiators include its strict adherence to the HTML5 parsing algorithm, extensive TypeScript type definitions, and its use as the underlying HTML parser in popular projects such as Cheerio, rehype, and Lit. It provides both a pull-stream based parser and a non-streaming API for parsing strings, along with customizable tree adapters.

npm install parse5
INSTALL
IMPORT
SIG · PARSE5
P
parse5
serializationjavascriptv8.0.1
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.

parse
import { parse } from 'parse5';
const parse = require('parse5');
parse5 is primarily consumed as an ES module. While CommonJS `require` might work in some setups, it's not the recommended or future-proof approach for modern Node.js/browser environments.
parseFragment
import { parseFragment } from 'parse5';
import parseFragment from 'parse5/lib/parser/parse-fragment';
For parsing HTML fragments, `parseFragment` is the correct named export. Directly importing from internal paths is not stable across versions.
serialize
import { serialize } from 'parse5';
import { serializeToString } from 'parse5';
The primary serialization function is `serialize`. There isn't a separate `serializeToString` export in the main library.
ParserStream
import { ParserStream } from 'parse5';
import { ParserStream } from 'parse5/lib/parser-stream';
The `ParserStream` class for streaming HTML parsing is available directly from the top-level `parse5` package. Importing from specific sub-paths is not guaranteed.
DefaultTreeAdapter
import { DefaultTreeAdapter } from 'parse5/lib/tree-adapters/default';
import { DefaultTreeAdapter } from 'parse5';
Tree adapter implementations are typically imported from their specific sub-paths, as they are not part of the main `parse5` exports. This allows for custom tree adapter usage.

Demonstrates basic full document parsing, HTML fragment parsing, serialization, and the usage of the streaming parser.

import { parse, serialize, parseFragment } from 'parse5'; import { createReadStream } from 'fs'; import { ParserStream } from 'parse5'; // Example 1: Parsing a full HTML document const htmlDoc = '<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Hello</h1><p>World</p></body></html>'; const document = parse(htmlDoc); console.log('Parsed Document Type:', document.nodeName); // #document // Example 2: Parsing an HTML fragment const htmlFragment = '<div><span>Fragment</span></div>'; const fragment = parseFragment(htmlFragment); console.log('Parsed Fragment Node Name:', fragment.nodeName); // #document-fragment // Example 3: Serializing a document back to HTML const serializedHtml = serialize(document); console.log('Serialized HTML:', serializedHtml); // Example 4: Using ParserStream for large files (simplified example) const largeHtmlFilePath = 'some-large-file.html'; // Replace with a real path if testing locally const parserStream = new ParserStream(); // In a real scenario, you'd pipe a readable stream to parserStream // For demonstration, we'll simulate a stream. // For actual usage, createReadStream(largeHtmlFilePath).pipe(parserStream); parserStream.write('<!DOCTYPE html><html><head></head><body>'); parserStream.write('<h1>Streaming</h1><p>Example</p>'); parserStream.end('</body></html>'); parserStream.on('finish', () => { console.log('Stream Parsed Document Type:', parserStream.document.nodeName); });
Debug
Known issues
breakingVersion 7.0.0 introduced significant internal changes and some breaking API changes. The project governance also changed, with maintenance by a team from Cheerio, rehype, and Lit.
fix
Review the parse5 v7.0.0 changelog and documentation for specific API adjustments. Pay attention to `TreeAdapter` interface changes and module exports.
affects: >=7.0.0
breakingVersion 6.0.0 changed the `TreeAdapter` interface, adding a new `updateNodeSourceCodeLocation` method. Custom tree adapters not implementing this method will break.
fix
If using a custom `TreeAdapter`, ensure it implements the `updateNodeSourceCodeLocation` method. For default usage, no action is required.
affects: >=6.0.0 <7.0.0
gotchaWhen parsing HTML fragments, `parseFragment` returns a DocumentFragment node. If you expect a single root element (like a `div`), you'll need to access `fragment.childNodes`.
fix
Always inspect the `nodeName` of the returned document or fragment to understand its structure. For fragments, iterate `fragment.childNodes` to find the desired elements.
affects: >=6.0.0
gotchaWhile parse5 is highly spec-compliant, direct manipulation of the AST can be complex. Libraries like Cheerio or rehype often provide a higher-level, more convenient API for traversal and manipulation built on top of parse5.
fix
For complex DOM manipulation or querying, consider using wrapper libraries like Cheerio (for jQuery-like syntax) or rehype (for AST transformations) that leverage parse5 internally.
affects: >=6.0.0
Errors
Common errors & fixes
TypeError: Cannot destructure property 'parse' of 'parse5__WEBPACK_IMPORTED_MODULE_0__' as it is undefined.
Incorrect import statement in a CommonJS context or when module resolution is misconfigured for ESM.
fix
Ensure you are using `import { parse } from 'parse5';` for ESM. For older CommonJS environments (though not recommended for parse5 v7+), `const parse = require('parse5').parse;` might be needed, but migrating to ESM is preferred.
Property 'someProperty' does not exist on type 'Document'.
Attempting to access properties specific to a `HTMLElement` (like `querySelector`) directly on the `Document` or `DocumentFragment` node returned by `parse` or `parseFragment` without type casting or checking node type.
fix
TypeScript users should correctly narrow down the node type or cast it. Remember that `parse` returns a `Document` node and `parseFragment` returns a `DocumentFragment` node. You might need to traverse to child nodes to find `Element` types.
Error: `parse5-parser-stream` is not exported from `parse5`.
Attempting to import `ParserStream` or related streaming utilities from a deprecated or incorrect path.
fix
As of recent versions, `ParserStream` is directly exported from the main `parse5` package. Use `import { ParserStream } from 'parse5';`.
Upgrade
Version history
8.0.1latest on npm
Audit
Dependencies
entitiesrequiredUsed for decoding and encoding HTML entities during parsing and serialization.
Agent activity
2 hits · last 30 days
node
2
Resources
parse5 — npm install parse5 · libregistry