{"id":11000,"library":"hast-util-from-string","title":"hast-util-from-string","description":"hast-util-from-string is a utility package within the unified and rehype ecosystem designed to efficiently set the plain-text value of a HAST (Hypertext Abstract Syntax Tree) node. It mimics the behavior of the DOM's `Node#textContent` setter, effectively replacing all children of a node with a single text node containing the provided string value. The current stable version is 3.0.1. As part of the unified collective, it follows a coordinated release cadence, often aligning major version bumps across related packages, though individual utilities receive minor and patch updates as needed. A key differentiator is its `textContent`-like behavior, contrasting with `hast-util-from-text` which emulates `innerText` and considers rendering specifics like line breaks and table cell separation. The library is ESM-only and fully typed with TypeScript.","status":"active","version":"3.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/rehypejs/rehype-minify#main","tags":["javascript","content","hast","hast-util","html","string","text","unist","util","typescript"],"install":[{"cmd":"npm install hast-util-from-string","lang":"bash","label":"npm"},{"cmd":"yarn add hast-util-from-string","lang":"bash","label":"yarn"},{"cmd":"pnpm add hast-util-from-string","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is ESM-only since v3 and requires Node.js 16+. CommonJS `require` is not supported.","wrong":"const fromString = require('hast-util-from-string')","symbol":"fromString","correct":"import { fromString } from 'hast-util-from-string'"},{"note":"For Deno environments, import directly from esm.sh specifying the major version.","symbol":"fromString (Deno)","correct":"import {fromString} from 'https://esm.sh/hast-util-from-string@3'"},{"note":"For browser environments, use esm.sh with the `?bundle` flag.","symbol":"fromString (Browser)","correct":"<script type=\"module\"> import {fromString} from 'https://esm.sh/hast-util-from-string@3?bundle' </script>"}],"quickstart":{"code":"import { h } from 'hastscript';\nimport { fromString } from 'hast-util-from-string';\n\n// Create an empty paragraph node\nconst p = h('p');\n\n// Set its plain-text value\nfromString(p, 'Alpha');\nconsole.log('Paragraph with new text:', JSON.stringify(p, null, 2));\n// Expected output: { type: 'element', tagName: 'p', properties: {}, children: [ { type: 'text', value: 'Alpha' } ] }\n\n// Create a div with existing children\nconst div = h('div', [h('b', 'Bold'), ' and ', h('i', 'italic'), '.']);\nconsole.log('Original div:', JSON.stringify(div, null, 2));\n\n// Overwrite its content with new plain text\nfromString(div, 'Charlie');\nconsole.log('Div with overwritten text:', JSON.stringify(div, null, 2));\n// Expected output: { type: 'element', tagName: 'div', properties: {}, children: [ { type: 'text', value: 'Charlie' } ] }","lang":"typescript","description":"Demonstrates how to initialize an empty HAST node and how to overwrite an existing node's children with a new plain-text string using `fromString`."},"warnings":[{"fix":"Migrate your project to use ES modules (`import`/`export`) and ensure you are running Node.js 16 or later. Update your `tsconfig.json` and `package.json` to reflect ESM usage.","message":"Starting with `hast-util-from-string` v3 (aligned with the unified ecosystem's v7 major release), the package became ESM-only. CommonJS `require()` statements will no longer work. Additionally, Node.js 16 or newer is now required.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure you keep a reference to the node passed to `fromString`. Do not rely on the function's return value. Example: `const myNode = h('p'); fromString(myNode, 'text'); console.log(myNode);`","message":"In `hast-util-from-string` v3 (part of the unified ecosystem's v7), the `fromString` function no longer returns the modified `node` argument. It now returns `undefined`. You must maintain a reference to the original node if you intend to use it after the function call.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Always use `rehype-sanitize` when processing untrusted HTML content in your rehype pipelines to strip out potentially malicious scripts or attributes.","message":"When working with HTML using rehype utilities, improper handling of user-provided content can lead to Cross-Site Scripting (XSS) vulnerabilities. This package, by setting text content, can mitigate some risks but does not parse HTML itself. Other rehype utilities, especially those dealing with raw HTML, can introduce XSS if not properly sanitized.","severity":"gotcha","affected_versions":"always"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement from `const { fromString } = require('hast-util-from-string')` to `import { fromString } from 'hast-util-from-string'`.","cause":"Attempting to import `hast-util-from-string` using CommonJS `require()` syntax in a project after upgrading to v3 or when running in an environment that enforces ESM.","error":"TypeError: require is not a function"},{"fix":"Modify your code to retain a reference to the HAST node passed into `fromString`. The function modifies the node in place. For example, `const node = h('p'); fromString(node, 'hello'); // node is now modified`.","cause":"After upgrading to `hast-util-from-string` v3, the `fromString` function no longer returns the modified HAST node (it returns `undefined`). Code expecting a return value will fail.","error":"TypeError: Cannot read properties of undefined (reading 'type') (or similar when trying to access properties of the return value)"}],"ecosystem":"npm"}