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-parserVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
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`.
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.
Use `element.appendChild(parse(htmlStringToAppend))` instead of `element.appendChild(htmlStringToAppend)`.
Upgrade your TypeScript version to `4.1.2` or newer if you encounter type-related compilation issues.
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.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.
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`.
Run `npm install node-html-parser` or `yarn add node-html-parser` to install the package. Verify the import path in your code.
No dependency data recorded yet.