{"id":13744,"library":"parse5-parser-stream","title":"Streaming HTML Parser (parse5-parser-stream)","description":"parse5-parser-stream is a dedicated package within the parse5 HTML parsing toolset, providing a streaming API for parsing HTML documents. It processes HTML incrementally as a Node.js Transform stream, making it suitable for handling large files or real-time data without loading the entire document into memory. The current stable version is 8.0.1. The broader parse5 project, which this package is part of, maintains an active development cadence with frequent patch and minor releases, alongside significant major version updates roughly every one to two years. Its key differentiators include strict adherence to the HTML5 specification, detailed tracking of source code locations for parsed elements (beneficial for tools like linters and formatters), and robust scripting support during parsing.","status":"active","version":"8.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/inikulin/parse5","tags":["javascript","parse5","parser","stream","streaming","typescript"],"install":[{"cmd":"npm install parse5-parser-stream","lang":"bash","label":"npm"},{"cmd":"yarn add parse5-parser-stream","lang":"bash","label":"yarn"},{"cmd":"pnpm add parse5-parser-stream","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core parsing logic and AST generation, which this streaming component utilizes.","package":"parse5","optional":false}],"imports":[{"note":"The main class is a named export. Default import or direct `require('pkg')` will not work as expected.","wrong":"const ParserStream = require('parse5-parser-stream');","symbol":"ParserStream","correct":"import { ParserStream } from 'parse5-parser-stream';"},{"note":"For CommonJS environments, destructure the named export. Direct `require('pkg')` without destructuring will yield an object, not the class constructor.","wrong":"const ParserStream = require('parse5-parser-stream').default;","symbol":"ParserStream","correct":"const { ParserStream } = require('parse5-parser-stream');"},{"note":"While ParserStream is from 'parse5-parser-stream', tree adapters like DefaultTreeAdapter are typically imported from the core 'parse5' package.","symbol":"DefaultTreeAdapter","correct":"import { DefaultTreeAdapter } from 'parse5';"}],"quickstart":{"code":"import { ParserStream } from 'parse5-parser-stream';\nimport { serialize } from 'parse5';\nimport { pipeline } from 'stream/promises';\n\nasync function parseHtmlStream(htmlContent: string) {\n  const parserStream = new ParserStream();\n  let document: any;\n\n  parserStream.on('end', () => {\n    document = parserStream.document;\n    console.log('Parsing complete. Document:', serialize(document));\n  });\n\n  parserStream.on('error', (err) => {\n    console.error('Parsing error:', err.message);\n  });\n\n  // A simple readable stream to feed content\n  const readableHtml = new (class extends require('stream').Readable {\n    _read() {\n      this.push(htmlContent);\n      this.push(null);\n    }\n  })();\n\n  try {\n    await pipeline(readableHtml, parserStream);\n    console.log('Successfully processed HTML stream.');\n  } catch (error) {\n    console.error('Stream pipeline failed:', error);\n  }\n}\n\n// Example usage\nconst sampleHtml = `<!DOCTYPE html>\n<html>\n  <head>\n    <title>Test Page</title>\n  </head>\n  <body>\n    <p>Hello, <span class=\"name\">World</span>!</p>\n    <!-- A comment -->\n  </body>\n</html>`;\n\nparseHtmlStream(sampleHtml);\n","lang":"typescript","description":"Demonstrates how to use `parse5-parser-stream` to parse HTML content using Node.js streams, including error handling and accessing the final document object. It shows piping a readable stream into the parser stream and then serializing the resulting document."},"warnings":[{"fix":"If using a custom `TreeAdapter`, ensure it implements `updateNodeSourceCodeLocation` as specified in the parse5 documentation for v6.x and higher.","message":"The `TreeAdapter` interface underwent a breaking change in v6.0.0, requiring a new `updateNodeSourceCodeLocation` method. Custom tree adapter implementations must be updated.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Carefully review the `parse5` v7.0.0 changelog for the core package and test existing code thoroughly, especially if you directly manipulate or inspect the AST produced by the parser stream.","message":"Version 7.0.0 introduced significant internal changes across the parse5 ecosystem. While `parse5-parser-stream` might not have had direct API changes explicitly listed as 'breaking' in its own changelog, underlying core `parse5` changes (e.g., node name string types) could affect consumers who deeply interact with the parsed AST.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Upgrade to `parse5-parser-stream@7.1.2` or newer for robust CommonJS support, or ensure your project is configured for ESM if staying on older versions.","message":"Prior to version 7.1.2, official CommonJS builds for `parse5-parser-stream` were not consistently available or fully supported, potentially leading to import issues in CJS-only Node.js environments.","severity":"gotcha","affected_versions":"<7.1.2"},{"fix":"Always attach an `error` listener to the `ParserStream` instance and ensure that all input data is written and the `end()` method (or piping a complete stream) is called when no more data is expected. Example: `parserStream.on('error', handleError); readableStream.pipe(parserStream);`.","message":"When consuming `parse5-parser-stream`, it's crucial to correctly handle the stream's `error` event and ensure the stream is `end`ed. Failure to do so can lead to silent failures, incomplete parsing, or hung processes.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'8.0.1':63 'activ':76 'adher':101 'alongsid':85 'api':25 'benefici':115 'broader':65 'cadenc':78 'code':110 'current':59 'data':50 'dedic':14 'detail':106 'develop':77 'differenti':98 'document':29,55 'element':114 'entir':54 'everi':91 'file':45 'formatt':121 'frequent':80 'handl':43 'html':2,19,28,32 'html5':104 'includ':99 'increment':33 'javascript':128 'key':97 'larg':44 'like':118 'linter':119 'load':52 'locat':111 'maintain':74 'major':87 'make':39 'memori':57 'minor':83 'node.js':36 'one':92 'packag':15,70 'pars':20,27,113,127 'parse5':5,9,18,66,129 'parse5-parser-stream':4,8 'parser':3,6,10,130 'part':72 'patch':81 'process':31 'project':67 'provid':22 'real':48 'real-tim':47 'releas':84 'robust':123 'rough':90 'script':124 'signific':86 'sourc':109 'specif':105 'stabl':60 'stream':1,7,11,24,38,131,132 'strict':100 'suitabl':41 'support':125 'time':49 'tool':117 'toolset':21 'track':107 'transform':37 'two':94 'typescript':133 'updat':89 'version':61,88 'within':16 'without':51 'year':95","created_at":"2026-04-20T01:56:04.965353+00:00","updated_at":"2026-04-20T01:56:04.965353+00:00","problems":[{"fix":"Use named imports: `import { ParserStream } from 'parse5-parser-stream';` for ESM, or destructuring `const { ParserStream } = require('parse5-parser-stream');` for CommonJS.","cause":"Attempting to `require('parse5-parser-stream')` directly or using `import ParserStream from 'parse5-parser-stream';`","error":"TypeError: ParserStream is not a constructor"},{"fix":"Access the `document` property only after the `parserStream` emits the `'end'` event, which signals that parsing is complete and the document tree is finalized.","cause":"Attempting to access `parserStream.document` before the parsing process has completed (i.e., before the 'end' event has fired).","error":"Cannot read properties of undefined (reading 'document')"}],"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":"https://parse5.js.org","github":"https://github.com/inikulin/parse5","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/parse5-parser-stream","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}}