Registry / serialization / node-html-parser

node-html-parser

JSON →
library7.1.0jsnpmunverified

node-html-parser, also known as Fast HTML Parser, is a high-performance JavaScript library designed to parse HTML and generate a simplified Document Object Model (DOM) tree. It prioritizes parsing speed for massive HTML files, meaning some malformed HTML might not be parsed with perfect fidelity, though it handles many common HTML errors like unclosed tags. The library is currently at version 7.1.0 and maintains an active release cadence, with multiple minor and patch updates in recent months. It ships with TypeScript types, supporting TypeScript projects from version 4.1.2 onwards. A key differentiator is its focus on parsing efficiency compared to other HTML parsers.

npm install node-html-parser
INSTALL
IMPORT
SIG · NODE-HTML-PARSER
N
node-html-parser
serializationjavascriptv7.1.0
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 'node-html-parser';
const parse = require('node-html-parser');
The `parse` function is a named export. For CommonJS, access it via `require('node-html-parser').parse` or assign the module to a variable and then access its `parse` property.
HTMLElement
import { HTMLElement } from 'node-html-parser';
import HTMLElement from 'node-html-parser';
HTMLElement is a named export, representing the class for parsed DOM elements. It's often used for type hinting in TypeScript.
parse (CommonJS)
const HTMLParser = require('node-html-parser'); const root = HTMLParser.parse(htmlString);
const { parse } = require('node-html-parser');
While destructuring `require` can work, the documentation examples often show assigning the module to a variable and then accessing `parse` as a property. Both methods are generally supported for named exports in CommonJS, but the module object might have a 'default' export if only `require('node-html-parser')` was used.

This quickstart demonstrates parsing an HTML string, querying elements, accessing properties like innerText, modifying content, and serializing the DOM back to an HTML string. It also shows a basic usage of parse options.

import { parse, HTMLElement } from 'node-html-parser'; const htmlString = '<ul id="list"><li>Hello World</li></ul><p>Another element</p>'; const root = parse(htmlString); // The parse() function adds an implicit wrapper node. The actual parsed content is its first child. console.log('Structure of the first child:', root.firstChild?.structure); // Querying for an element by ID const listElement: HTMLElement | null = root.querySelector('#list'); if (listElement) { console.log('List element found:', listElement.tagName); console.log('List raw attributes:', listElement.rawAttrs); console.log('List inner text:', listElement.innerText); } // Modifying content and serializing back to string if (listElement) { listElement.set_content('<li>New Item 1</li><li>New Item 2</li>'); } console.log('Modified HTML:', root.toString()); // Example of parsing options const htmlWithComment = '<!-- This is a comment --><div>Hello</div>'; const parsedWithComments = parse(htmlWithComment, { comment: true }); console.log('Parsed with comments:', parsedWithComments.toString());
Debug
Known issues
breakingVersion 7.0.0 changed its JavaScript target to ES6 (ES2015). Projects using older Node.js versions or build environments targeting ES5 may encounter compatibility issues.
fix
Ensure your Node.js environment is compatible with ES6+ or configure your build tools (e.g., Babel) to transpile node-html-parser if targeting older runtimes.
affects: >=7.0.0
gotchaDue to its performance-first design, node-html-parser may not correctly parse all forms of highly malformed HTML. While common errors are covered, complex or extremely invalid markup might lead to unexpected DOM structures.
fix
For highly malformed or extremely complex HTML, consider pre-processing the HTML or using a more forgiving, but potentially slower, parser if strict HTML5 compliance is critical.
affects: >=1.0.0
gotchaThe `parse()` function implicitly adds a wrapper node around the input HTML. This means `root.firstChild` typically represents the first actual element of your parsed HTML, not `root` itself.
fix
Always access the desired elements via `root.querySelector` or iterate `root.childNodes` if you expect multiple top-level elements. If you know there's only one main root element in your HTML, use `root.firstChild`.
affects: >=1.0.0
gotchaOptions like `lowerCaseTagName: false` and `comment: false` (to retrieve comments) can negatively impact parsing performance. Enable them only when necessary.
fix
Review your `parse` options. By default, `lowerCaseTagName` is true and `comment` is false for optimal performance. Only override if you explicitly need original tag casing or comment nodes.
affects: >=1.0.0
gotchaWhen dynamically inserting HTML content into an existing `HTMLElement`, the string content must first be parsed into an `HTMLElement` instance using `parse()` before being appended. Direct string appending will not work as expected.
fix
Use `element.appendChild(parse(htmlStringToAppend))` instead of `element.appendChild(htmlStringToAppend)`.
affects: >=1.0.0
gotchaFor TypeScript projects, node-html-parser requires a minimum TypeScript version of `^4.1.2`. Using an older version may lead to compilation errors related to type definitions.
fix
Upgrade your TypeScript version to `4.1.2` or newer if you encounter type-related compilation issues.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (0, _nodeHtmlParser.parse) is not a function
Incorrect CommonJS `require` syntax when trying to import `parse` as a named export from an ESM-first package or when the module's main export structure differs from expectation.
fix
For CommonJS, use `const HTMLParser = require('node-html-parser'); const root = HTMLParser.parse(htmlString);` or `const { parse } = require('node-html-parser');` if `parse` is a named export. The library primarily uses named exports.
HTML Parser does not parse full content or only see a very small portion of HTML
The input provided to the `parse` function is not the raw HTML string, but potentially a file path, URI, or other non-string data. The `parse` function expects a string containing the HTML markup.
fix
Ensure you are reading the HTML file content into a string variable before passing it to `parse()`. For example, using `fs.readFileSync` or an HTTP client to get the HTML content as a string.
100% cpu while parsing document
Parsing extremely large or overly complex HTML documents can lead to the parser consuming excessive CPU and potentially hanging, especially with deeply nested or poorly structured markup.
fix
Break down large HTML documents into smaller chunks before parsing, or consider optimizing the HTML source. If the issue is with a specific complex tag structure, check for open issues in the library's GitHub repository or try adjusting parse options like `preserveTagNesting` or `closeAllOnClosing`.
Cannot find module 'node-html-parser'
The package `node-html-parser` is not installed or the import/require path is incorrect.
fix
Run `npm install node-html-parser` or `yarn add node-html-parser` to install the package. Verify the import path in your code.
Upgrade
Version history
7.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources