API Reference

htmlparser2

A fast and forgiving HTML and XML parser with a SAX-style API and a DOM builder.

Official Documentation ↗
CompatibilityThis documentation covers version v9. Check the official docs for the latest API surface.

Installation

npmnpm install htmlparser2

Methods

htmlparser2 powers cheerio and offers both an incremental Parser and a domhandler tree builder.

Key Features

  • Forgiving parsing of malformed HTML
  • SAX events plus a full DOM builder
  • Entity decoding backed by the entities package

Usage Notes

Pin a version and check the official docs for the API surface you depend on before upgrading.

API reference evidence

Fetched from official sources on 2026-11-08T17:51:20.000Z via WebFetch https://raw.githubusercontent.com/fb55/htmlparser2/master/src/index.ts.

FAQ

How does it compare to cheerio?
cheerio wraps htmlparser2 to provide a jQuery-like API. Use htmlparser2 directly when you want raw parser speed without selector sugar.
Does it decode HTML entities?
Yes. Entity decoding uses the entities package, and you can disable it with decodeEntities: false when you need raw text.
Is it a streaming parser?
Yes. The Parser class is incremental, so you can push chunks and handle events as they arrive.