Registry / serialization / hast-util-from-parse5

hast-util-from-parse5

JSON →
library8.0.3jsnpmunverified

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.

npm install hast-util-from-parse5
INSTALL
IMPORT
SIG · HAST-UTIL-FROM-PAR
H
hast-util-from-parse5
serializationjavascriptv8.0.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fromParse5
import { fromParse5 } from 'hast-util-from-parse5'
const fromParse5 = require('hast-util-from-parse5')
This package is ESM-only since v7.0.0. CommonJS `require` is not supported. There is no default export.
fromParse5
import { fromParse5 } from 'hast-util-from-parse5'
import fromParse5 from 'hast-util-from-parse5'
The package uses named exports exclusively; there is no default export.
Options
import type { Options } from 'hast-util-from-parse5'
import { Options } from 'hast-util-from-parse5'
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.

Demonstrates parsing an HTML file using `parse5`, converting the `parse5` AST to a `hast` AST, and inspecting the resulting `hast` tree.

import { fromParse5 } from 'hast-util-from-parse5'; import { parse } from 'parse5'; import { read } from 'to-vfile'; import { inspect } from 'unist-util-inspect'; import path from 'path'; import { fileURLToPath } from 'url'; import { writeFile } from 'fs/promises'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); async function runExample() { const exampleHtmlPath = path.join(__dirname, 'example.html'); // Create a dummy example.html for the quickstart to run await writeFile(exampleHtmlPath, '<!doctype html><title>Hello!</title><h1 id="world">World!<!--after-->\n'); const file = await read(exampleHtmlPath); // Parse the HTML string into a parse5 AST const p5ast = parse(String(file), { sourceCodeLocationInfo: true }); // Transform the parse5 AST to a hast AST const hast = fromParse5(p5ast, { file }); console.log('--- hast AST ---'); // Inspect the resulting hast AST console.log(inspect(hast)); } runExample().catch(console.error);
Debug
Known issues
breakingVersion 8.0.0 and above require Node.js 16 or newer. Older Node.js versions are not supported.
fix
Upgrade your Node.js environment to version 16 or later. For projects requiring older Node.js, use `hast-util-from-parse5@7`.
affects: >=8.0.0
breakingThe package transitioned to ESM (ECMAScript Modules) only in version 7.0.0. CommonJS `require()` is no longer supported.
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.
affects: >=7.0.0
breakingIn 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}`).
fix
Update calls to `fromParse5` from `fromParse5(tree, file)` to `fromParse5(tree, {file: file})`.
affects: >=8.0.0
breakingVersion 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.
fix
Run `npm update` or manually update relevant `@types/*` packages, especially `@types/hast`, in your project.
affects: >=8.0.0
gotchaThe 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.
fix
Always use named imports: `import { fromParse5 } from 'hast-util-from-parse5'`.
affects: >=7.0.0
breakingVersion 7.0.0 removed support for non-HTML doctypes, focusing solely on HTML parsing and conversion.
fix
Ensure your input `parse5` AST represents HTML documents. If non-HTML doctypes were previously handled, consider pre-processing or using a different utility.
affects: >=7.0.0
Errors
Common errors & fixes
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.
Attempting to use CommonJS `require()` to import an ESM-only package.
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`).
TypeError: The 'file' option must be an object with a 'file' property, got {…}
Using the deprecated direct `VFile` option instead of the object format for the `file` option with `fromParse5`.
fix
Update your call from `fromParse5(ast, myFile)` to `fromParse5(ast, { file: myFile })`.
TypeError: (0 , hast_util_from_parse5__WEBPACK_IMPORTED_MODULE_0__.fromParse5) is not a function
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.
fix
Verify that you are using a named import: `import { fromParse5 } from 'hast-util-from-parse5'`. Ensure your build configuration correctly handles ESM imports.
SyntaxError: Named export 'fromParse5' not found. The requested module 'hast-util-from-parse5' does not provide an export named 'fromParse5'
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.
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.
Upgrade
Version history
8.0.3latest on npm
Audit
Dependencies
parse5requiredCore dependency; this utility transforms ASTs generated by `parse5`.
hastrequiredThe output format of this utility; crucial for working with the resulting AST.
Agent activity
4 hits · last 30 days
node
4
Resources