# domhandler

> Handler for htmlparser2 that turns pages into a dom

Latest version **6.0.1** (published 2026-03-17) · BSD-2-Clause license · 0 weekly downloads

## Install

```sh
npm install domhandler
pnpm add domhandler
yarn add domhandler
bun add domhandler
```

## Health

**Score 70/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.0.1 |
| Published | 2026-03-17 |
| First published | 2012-08-14 |
| Weekly downloads | 0 |
| License | BSD-2-Clause |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20.19.0 |
| Dependencies | 1 |
| Unpacked size | 36.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 368 |
| Author | Felix Boehm |
| Maintainers | fb55 |
| Keywords | dom, htmlparser2 |

## Links

- npm: https://www.npmjs.com/package/domhandler
- Repository: https://github.com/fb55/domhandler
- Homepage: https://github.com/fb55/domhandler#readme
- Issues: https://github.com/fb55/domhandler/issues
- Funding: https://github.com/fb55/domhandler?sponsor=1
- npm.io page: https://npm.io/package/domhandler

## Dependencies (1)

- [domelementtype](https://npm.io/package/domelementtype.md) ^3.0.0

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 6.0.1 (latest) — 2026-03-17
- 6.0.0 — 2026-03-17
- 5.0.3 — 2022-04-30
- 5.0.2 — 2022-04-09
- 5.0.1 — 2022-04-08
- 5.0.0 — 2022-04-08
- 4.3.1 — 2022-03-19
- 4.3.0 — 2021-11-30
- 4.2.2 — 2021-08-29
- 4.2.1 — 2021-08-29
- 4.2.0 — 2021-04-16
- 4.1.0 — 2021-04-01
- 4.0.0 — 2020-12-02
- 3.3.0 — 2020-10-03
- 3.2.0 — 2020-09-25
- … 20 more at https://npm.io/package/domhandler/versions

## README

# domhandler [![Node.js CI](https://github.com/fb55/domhandler/actions/workflows/nodejs-test.yml/badge.svg)](https://github.com/fb55/domhandler/actions/workflows/nodejs-test.yml)

The DOM handler creates a tree containing all nodes of a page.
The tree can be manipulated using the [domutils](https://github.com/fb55/domutils)
or [cheerio](https://github.com/cheeriojs/cheerio) libraries and
rendered using [dom-serializer](https://github.com/cheeriojs/dom-serializer) .

## Usage

```javascript
const handler = new DomHandler([ <func> callback(err, dom), ] [ <obj> options ]);
// const parser = new Parser(handler[, options]);
```

Available options are described below.

## Example

```javascript
const { Parser } = require("htmlparser2");
const { DomHandler } = require("domhandler");
const rawHtml =
    "Xyz <script language= javascript>var foo = '<<bar>>';</script><!--<!-- Waah! -- -->";
const handler = new DomHandler((error, dom) => {
    if (error) {
        // Handle error
    } else {
        // Parsing completed, do something
        console.log(dom);
    }
});
const parser = new Parser(handler);
parser.write(rawHtml);
parser.end();
```

Output:

```javascript
[
    {
        data: "Xyz ",
        type: "text",
    },
    {
        type: "script",
        name: "script",
        attribs: {
            language: "javascript",
        },
        children: [
            {
                data: "var foo = '<bar>';<",
                type: "text",
            },
        ],
    },
    {
        data: "<!-- Waah! -- ",
        type: "comment",
    },
];
```

## Option: `withStartIndices`

Add a `startIndex` property to nodes.
When the parser is used in a non-streaming fashion, `startIndex` is an integer
indicating the position of the start of the node in the document.
The default value is `false`.

## Option: `withEndIndices`

Add an `endIndex` property to nodes.
When the parser is used in a non-streaming fashion, `endIndex` is an integer
indicating the position of the end of the node in the document.
The default value is `false`.

---

License: BSD-2-Clause

## Security contact information

To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security).
Tidelift will coordinate the fix and disclosure.

---
_Source: https://npm.io/package/domhandler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
