{"id":13286,"library":"hast-util-from-parse5","title":"HAST Utility for Parse5 Conversion","description":"hast-util-from-parse5 is a specialized utility within the unified.js ecosystem designed to transform an AST (Abstract Syntax Tree) generated by the `parse5` HTML parser into a HAST (Hypertext Abstract Syntax Tree). This package is crucial for developers who parse HTML using `parse5` and then wish to process the resulting tree using the vast array of HAST-compatible tools and plugins. The current stable version is 8.0.3, with releases typically focusing on dependency updates, minor bug fixes, and periodic major versions to align with Node.js LTS releases and significant internal changes (e.g., ESM migration in v7, Node.js 16 requirement in v8). Its key differentiator is its role as a bridge between the widely used `parse5` parser and the expressive `hast` AST format, enabling seamless integration into unified processor workflows for tasks like linting, transforming, or compiling HTML content.","status":"active","version":"8.0.3","language":"javascript","source_language":"en","source_url":"https://github.com/syntax-tree/hast-util-from-parse5","tags":["javascript","ast","change","hast-util","hast","transform","unist","utility","util","typescript"],"install":[{"cmd":"npm install hast-util-from-parse5","lang":"bash","label":"npm"},{"cmd":"yarn add hast-util-from-parse5","lang":"bash","label":"yarn"},{"cmd":"pnpm add hast-util-from-parse5","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core dependency; this utility transforms ASTs generated by `parse5`.","package":"parse5","optional":false},{"reason":"The output format of this utility; crucial for working with the resulting AST.","package":"hast","optional":false}],"imports":[{"note":"This package is ESM-only since v7.0.0. CommonJS `require` is not supported. There is no default export.","wrong":"const fromParse5 = require('hast-util-from-parse5')","symbol":"fromParse5","correct":"import { fromParse5 } from 'hast-util-from-parse5'"},{"note":"The package uses named exports exclusively; there is no default export.","wrong":"import fromParse5 from 'hast-util-from-parse5'","symbol":"fromParse5","correct":"import { fromParse5 } from 'hast-util-from-parse5'"},{"note":"Import `Options` and other types using `import type` for clarity and to avoid bundling issues in some environments. While `import { Options }` might work, `import type` is preferred for type-only imports.","wrong":"import { Options } from 'hast-util-from-parse5'","symbol":"Options","correct":"import type { Options } from 'hast-util-from-parse5'"}],"quickstart":{"code":"import { fromParse5 } from 'hast-util-from-parse5';\nimport { parse } from 'parse5';\nimport { read } from 'to-vfile';\nimport { inspect } from 'unist-util-inspect';\nimport path from 'path';\nimport { fileURLToPath } from 'url';\nimport { writeFile } from 'fs/promises';\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\nasync function runExample() {\n  const exampleHtmlPath = path.join(__dirname, 'example.html');\n  // Create a dummy example.html for the quickstart to run\n  await writeFile(exampleHtmlPath, '<!doctype html><title>Hello!</title><h1 id=\"world\">World!<!--after-->\\n');\n\n  const file = await read(exampleHtmlPath);\n  // Parse the HTML string into a parse5 AST\n  const p5ast = parse(String(file), { sourceCodeLocationInfo: true });\n  // Transform the parse5 AST to a hast AST\n  const hast = fromParse5(p5ast, { file });\n\n  console.log('--- hast AST ---');\n  // Inspect the resulting hast AST\n  console.log(inspect(hast));\n}\n\nrunExample().catch(console.error);\n","lang":"typescript","description":"Demonstrates parsing an HTML file using `parse5`, converting the `parse5` AST to a `hast` AST, and inspecting the resulting `hast` tree."},"warnings":[{"fix":"Upgrade your Node.js environment to version 16 or later. For projects requiring older Node.js, use `hast-util-from-parse5@7`.","message":"Version 8.0.0 and above require Node.js 16 or newer. Older Node.js versions are not supported.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Migrate your project to use ESM `import` statements. Ensure your `package.json` has `\"type\": \"module\"` or use `.mjs` file extensions for modules importing this package.","message":"The package transitioned to ESM (ECMAScript Modules) only in version 7.0.0. CommonJS `require()` is no longer supported.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Update calls to `fromParse5` from `fromParse5(tree, file)` to `fromParse5(tree, {file: file})`.","message":"In v8.0.0, the `file` option for `fromParse5` changed its signature. Previously, you could pass a `VFile` directly; now, it must be an object with a `file` property (e.g., `{file: myFile}`).","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Run `npm update` or manually update relevant `@types/*` packages, especially `@types/hast`, in your project.","message":"Version 8.0.0 updated its dependencies on `@types/hast` and other utilities. Users might need to explicitly update their own type dependencies to ensure compatibility.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Always use named imports: `import { fromParse5 } from 'hast-util-from-parse5'`.","message":"The package exclusively uses named exports and does not provide a default export. Attempting to import it as a default export will result in an error.","severity":"gotcha","affected_versions":">=7.0.0"},{"fix":"Ensure your input `parse5` AST represents HTML documents. If non-HTML doctypes were previously handled, consider pre-processing or using a different utility.","message":"Version 7.0.0 removed support for non-HTML doctypes, focusing solely on HTML parsing and conversion.","severity":"breaking","affected_versions":">=7.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change `const fromParse5 = require('hast-util-from-parse5')` to `import { fromParse5 } from 'hast-util-from-parse5'` in your module, and ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json`).","cause":"Attempting to use CommonJS `require()` to import an ESM-only package.","error":"Error [ERR_REQUIRE_ESM]: require() of ES module /node_modules/hast-util-from-parse5/index.js not supported. Instead change the require of index.js in /your/project/to.js to a dynamic import() or change 'to.js' to a module."},{"fix":"Update your call from `fromParse5(ast, myFile)` to `fromParse5(ast, { file: myFile })`.","cause":"Using the deprecated direct `VFile` option instead of the object format for the `file` option with `fromParse5`.","error":"TypeError: The 'file' option must be an object with a 'file' property, got {…}"},{"fix":"Verify that you are using a named import: `import { fromParse5 } from 'hast-util-from-parse5'`. Ensure your build configuration correctly handles ESM imports.","cause":"This error often occurs in transpiled environments (like Webpack) when a named export is incorrectly imported as a default, or when using an incompatible import method.","error":"TypeError: (0 , hast_util_from_parse5__WEBPACK_IMPORTED_MODULE_0__.fromParse5) is not a function"},{"fix":"Confirm your Node.js version is 16+ and your package version is 7.0.0 or higher. Ensure you are using `import { fromParse5 } from 'hast-util-from-parse5'` and not a default import. Re-installing `node_modules` might also help resolve corrupt caches.","cause":"This typically means you are trying to import `fromParse5` from a version that might not export it, or there's a bundling issue. It can also happen if you implicitly try to `require` an ESM module.","error":"SyntaxError: Named export 'fromParse5' not found. The requested module 'hast-util-from-parse5' does not provide an export named 'fromParse5'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null}