{"id":13627,"library":"node-html-better-parser","title":"Node HTML Better Parser","description":"Node HTML Better Parser is a high-performance HTML parser for Node.js and TypeScript, currently at version 1.5.8 and last published 6 months ago. It serves as a fork of `fast-html-parser`, prioritizing speed to process large HTML files efficiently. It generates a simplified Document Object Model (DOM) and includes basic element query support. A key differentiator of this fork is its focus on providing a simpler API for editing HTML and its attributes, which was a primary motivation for its creation. While highly optimized for performance, it may not correctly parse all malformed HTML, though it handles common errors like missing closing tags for `<li>` or `<td>`. The library offers configurable options to retrieve content from `<script>`, `<style>`, `<pre>`, and comments, but users should note these options can slightly impact parsing performance.","status":"active","version":"1.5.8","language":"javascript","source_language":"en","source_url":"https://github.com/Sharcoux/node-html-parser","tags":["javascript","fast html parser nodejs typescript","typescript"],"install":[{"cmd":"npm install node-html-better-parser","lang":"bash","label":"npm"},{"cmd":"yarn add node-html-better-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-html-better-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary function for parsing HTML. For TypeScript and modern Node.js, use named ESM import. CommonJS `require` is also supported, as shown in the README.","wrong":"const parse = require('node-html-better-parser').parse;","symbol":"parse","correct":"import { parse } from 'node-html-better-parser';"},{"note":"This is the typical CommonJS `require` pattern where the module export is an object. In ESM, prefer named imports like `{ parse }`.","wrong":"import HTMLParser from 'node-html-better-parser';","symbol":"HTMLParser (CommonJS style)","correct":"const HTMLParser = require('node-html-better-parser');\n// then use HTMLParser.parse(data)"},{"note":"Import `HTMLElement` as a type for type annotations when working with parsed nodes, particularly in TypeScript.","symbol":"HTMLElement","correct":"import { HTMLElement } from 'node-html-better-parser';"}],"quickstart":{"code":"import { parse } from 'node-html-better-parser';\n\nconst htmlString = '<!DOCTYPE html>\\n<html lang=\"en\">\\n<head>\\n  <meta charset=\"UTF-8\">\\n  <title>Sample Page</title>\\n  <style>body { font-family: sans-serif; }</style>\\n</head>\\n<body>\\n  <ul id=\"main-list\">\\n    <li>Hello World</li>\\n    <li class=\"item\">Another item</li>\\n  </ul>\\n  <script>console.log(\"script content\");</script>\\n</body>\\n</html>';\n\n// Parse the HTML, enabling script and style content retrieval\nconst root = parse(htmlString, { script: true, style: true });\n\n// Access the root node's children (the <html> tag in this case)\nconsole.log('Root children structure:', root.firstChild?.structure);\n\n// Query for an element by ID\nconst mainList = root.querySelector('#main-list');\nif (mainList) {\n  console.log('\\nFound #main-list:');\n  console.log('  Tag Name:', mainList.tagName);\n  console.log('  Raw Attributes:', mainList.rawAttrs);\n  console.log('  Text Content:', mainList.text);\n\n  // Edit content of the list\n  mainList.set_content('<li>New first item</li><li>New second item</li>');\n  console.log('\\nList after set_content:', mainList.toString());\n}\n\n// Query for an element by class name\nconst item = root.querySelector('.item'); // Note: This will find 'Another item' before set_content\nif (item) {\n  console.log('\\nFirst element with class \"item\":', item.toString());\n}\n\n// Get the content of the script tag\nconst scriptTag = root.querySelector('script');\nif (scriptTag) {\n  console.log('\\nScript Content:', scriptTag.text);\n}\n\nconsole.log('\\nFull modified HTML:', root.toString());","lang":"typescript","description":"This quickstart demonstrates parsing an HTML string, querying elements by ID and class, accessing their properties, retrieving script and style content using options, and modifying an element's content, then printing the resulting HTML."},"warnings":[{"fix":"Ensure input HTML is as well-formed as possible. For highly unpredictable or malicious HTML, consider pre-processing or using a more robust, but potentially slower, parser that aims for W3C compliance like `parse5`.","message":"Node HTML Better Parser prioritizes performance, meaning some highly malformed HTML might not be parsed correctly, although it handles common HTML4-style errors (e.g., unclosed `<li>` or `<td>` tags). Always validate input if robustness against arbitrary malformed HTML is critical.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Only enable parsing options (e.g., `script: true`, `style: true`) if you explicitly need to retrieve their content. Avoid `lowerCaseTagName` unless strictly necessary for your application's logic.","message":"Enabling certain parsing options (like `lowerCaseTagName`, `script`, `style`, `pre`, or `comment`) can significantly hurt performance. `lowerCaseTagName` is noted to hurt performance heavily, while others slightly impact it.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If frequently accessing `text`, be aware of the initial performance cost. For operations requiring the raw, unescaped text content, use `text`. If performance is paramount and escaped text is acceptable, `rawText` might be more suitable or pre-calculate `text` once if needed repeatedly.","message":"The `text` property of an `HTMLElement` is described as 'slow for the first time'. This implies an internal caching mechanism that incurs a performance hit on the initial access.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'1.5.8':23 '6':27 'ago':29 'api':75 'attribut':81 'basic':58 'better':3,7 'close':110 'common':106 'configur':117 'content':121 'correct':98 'creation':89 'current':20 'differenti':64 'document':52 'dom':55 'edit':77 'effici':47 'element':59 'error':107 'fast':37 'fast-html-pars':36 'file':46 'focus':70 'fork':34,67 'generat':49 'handl':105 'high':12,91 'high-perform':11 'html':2,6,14,38,45,78,102 'includ':57 'key':63 'larg':44 'last':25 'librari':115 'like':108 'malform':101 'may':96 'miss':109 'model':54 'month':28 'motiv':86 'node':1,5 'node.js':17 'object':53 'offer':116 'optim':92 'option':118 'pars':99 'parser':4,8,15,39 'perform':13,94 'primari':85 'priorit':40 'process':43 'provid':72 'publish':26 'queri':60 'retriev':120 'serv':31 'simpler':74 'simplifi':51 'speed':41 'support':61 'tag':111 'though':103 'typescript':19 'version':22","created_at":"2026-04-20T01:55:28.346413+00:00","updated_at":"2026-04-20T01:55:28.346413+00:00","problems":[{"fix":"For ESM, use `import { parse } from 'node-html-better-parser';`. For CommonJS, use `const HTMLParser = require('node-html-better-parser');` and then `HTMLParser.parse(html)`. Do not use `const { parse } = require('node-html-better-parser');` directly if the module's main export is not a direct object with a `parse` property when using CommonJS (though for this library, it usually works).","cause":"Attempting to use `parse` with an incorrect import statement, often when mixing CommonJS `require` with an ESM named import expectation, or vice-versa, or trying to use `require('pkg')` directly as a function.","error":"TypeError: parse is not a function"},{"fix":"Always check the return value of `parse` and `querySelector` calls. The `parse` function returns a fictive root node. Access its children via `root.childNodes` or `root.firstChild` to ensure you're querying actual HTML elements. For example, `const root = parse(html); if (root && root.querySelector) { /* ... */ }`.","cause":"This typically occurs when `querySelector` is called on a `null` or `undefined` object, often because the `parse` function returned an empty or unexpected root, or a preceding `querySelector` failed to find a matching element.","error":"TypeError: Cannot read properties of undefined (reading 'querySelector')"}],"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/Sharcoux/node-html-parser","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/node-html-better-parser","openapi_spec":null,"status_page":null,"smithery":null,"categories":["web-framework","serialization","http-networking"],"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}}