{"id":13628,"library":"node-html-parser","title":"Fast HTML Parser","description":"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.","status":"active","version":"7.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/taoqf/node-fast-html-parser","tags":["javascript","html","parser","nodejs","typescript"],"install":[{"cmd":"npm install node-html-parser","lang":"bash","label":"npm"},{"cmd":"yarn add node-html-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-html-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const parse = require('node-html-parser');","symbol":"parse","correct":"import { parse } from 'node-html-parser';"},{"note":"HTMLElement is a named export, representing the class for parsed DOM elements. It's often used for type hinting in TypeScript.","wrong":"import HTMLElement from 'node-html-parser';","symbol":"HTMLElement","correct":"import { HTMLElement } from 'node-html-parser';"},{"note":"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.","wrong":"const { parse } = require('node-html-parser');","symbol":"parse (CommonJS)","correct":"const HTMLParser = require('node-html-parser');\nconst root = HTMLParser.parse(htmlString);"}],"quickstart":{"code":"import { parse, HTMLElement } from 'node-html-parser';\n\nconst htmlString = '<ul id=\"list\"><li>Hello World</li></ul><p>Another element</p>';\n\nconst root = parse(htmlString);\n\n// The parse() function adds an implicit wrapper node. The actual parsed content is its first child.\nconsole.log('Structure of the first child:', root.firstChild?.structure);\n\n// Querying for an element by ID\nconst listElement: HTMLElement | null = root.querySelector('#list');\nif (listElement) {\n  console.log('List element found:', listElement.tagName);\n  console.log('List raw attributes:', listElement.rawAttrs);\n  console.log('List inner text:', listElement.innerText);\n}\n\n// Modifying content and serializing back to string\nif (listElement) {\n  listElement.set_content('<li>New Item 1</li><li>New Item 2</li>');\n}\nconsole.log('Modified HTML:', root.toString());\n\n// Example of parsing options\nconst htmlWithComment = '<!-- This is a comment --><div>Hello</div>';\nconst parsedWithComments = parse(htmlWithComment, { comment: true });\nconsole.log('Parsed with comments:', parsedWithComments.toString());\n","lang":"typescript","description":"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."},"warnings":[{"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.","message":"Version 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.","severity":"breaking","affected_versions":">=7.0.0"},{"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.","message":"Due 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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`.","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"Options like `lowerCaseTagName: false` and `comment: false` (to retrieve comments) can negatively impact parsing performance. Enable them only when necessary.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use `element.appendChild(parse(htmlStringToAppend))` instead of `element.appendChild(htmlStringToAppend)`.","message":"When 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade your TypeScript version to `4.1.2` or newer if you encounter type-related compilation issues.","message":"For 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'4.1.2':95 '7.1.0':69 'activ':73 'also':8 'cadenc':75 'common':57 'compar':106 'current':66 'design':21 'differenti':99 'document':29 'dom':32 'effici':105 'error':59 'fast':1,11 'fidel':52 'file':41 'focus':102 'generat':26 'handl':55 'high':17 'high-perform':16 'html':2,6,12,24,40,45,58,109,112 'javascript':19,111 'key':98 'known':9 'librari':20,64 'like':60 'maintain':71 'malform':44 'mani':56 'massiv':39 'mean':42 'might':46 'minor':78 'model':31 'month':84 'multipl':77 'node':5 'node-html-pars':4 'nodej':114 'object':30 'onward':96 'pars':23,36,49,104 'parser':3,7,13,110,113 'patch':80 'perfect':51 'perform':18 'priorit':35 'project':92 'recent':83 'releas':74 'ship':86 'simplifi':28 'speed':37 'support':90 'tag':62 'though':53 'tree':33 'type':89 'typescript':88,91,115 'unclos':61 'updat':81 'version':68,94","created_at":"2026-04-20T01:55:29.416008+00:00","updated_at":"2026-04-20T01:55:29.416008+00:00","problems":[{"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.","cause":"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.","error":"TypeError: (0, _nodeHtmlParser.parse) is not a function"},{"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.","cause":"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.","error":"HTML Parser does not parse full content or only see a very small portion of HTML"},{"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`.","cause":"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.","error":"100% cpu while parsing document"},{"fix":"Run `npm install node-html-parser` or `yarn add node-html-parser` to install the package. Verify the import path in your code.","cause":"The package `node-html-parser` is not installed or the import/require path is incorrect.","error":"Cannot find module 'node-html-parser'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/taoqf/node-fast-html-parser","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/node-html-parser","openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization","web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}