htmlparser2 is a high-performance, event-driven HTML/XML parser for JavaScript and TypeScript environments. It is currently at stable version 12.0.0 and maintains an active release cadence with frequent updates, often aligning with WHATWG specifications. The library prioritizes speed and efficiency, making it suitable for tasks like web scraping, content transformation, and processing RSS/Atom feeds. While fast and forgiving, it takes some shortcuts compared to strictly spec-compliant parsers like `parse5`, which might lead to different parsing results for highly malformed HTML. It integrates with an ecosystem of related packages like `domhandler` for DOM construction and `css-select` for querying.
npm install htmlparser2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the event-driven parsing capabilities of htmlparser2 by creating a `Parser` instance and feeding it HTML content. It logs opening tags, text content, and closing tags, illustrating the callback interface.
Update your project to use ES Modules (e.g., `"type": "module"` in `package.json`) and replace all `require('htmlparser2')` with `import { ... } from 'htmlparser2';`.Ensure your project's Node.js environment is updated to version 20.19.0 or newer.
Review existing code that processes content within these tags, as the parsing behavior for their children will have changed. Content previously parsed as HTML will now be treated as raw text.
Test parsing of HTML with complex or malformed entities in attributes to ensure the new behavior does not negatively impact your application's logic. Adjust expectations for attribute values as necessary.
Migrate your feed parsing logic. The documentation or past changelogs for v8.0.0 should provide guidance on how to replace `FeedHandler` functionality, typically by using a generic handler with `parseDocument` and `domutils`.
Upgrade your project's TypeScript dependency to version 4.5 or newer.
If strict HTML compliance is critical, evaluate if htmlparser2's parsing behavior meets your requirements, especially with highly malformed or unusual HTML inputs. Consider using `parse5` if strictness is paramount.
Change `const htmlparser2 = require('htmlparser2');` to `import * as htmlparser2 from 'htmlparser2';` or `import { Parser } from 'htmlparser2';`. Ensure your `package.json` has `"type": "module"` if running in Node.js.Refactor your code to no longer use `FeedHandler`. Instead, use the `Parser` class with custom handlers or the `parseDocument` function along with `domutils` and `domhandler` to process feeds.
Ensure `htmlparser2` is installed. If using an older TypeScript version (<4.5), upgrade it as v8.0.0+ requires TS >= 4.5. Verify your `tsconfig.json` `moduleResolution` is set appropriately for ESM (e.g., `"node16"` or `"bundler"`).
For very large documents, consider using the event-driven `Parser` directly with custom handlers to process chunks incrementally, rather than building a complete DOM tree with `parseDocument`. Increase Node.js stack size (`--stack-size=N`) as a temporary measure if acceptable.