Troubleshooting rehype

Each section starts with the symptom, then the cause and the fix. Error messages are quoted exactly as Node.js 22 printed them in our tests.

“does not provide an export named 'default'”

error-default-import.mjs
// @expect-error: rehype has no default export, so this line throws.
import rehype from 'rehype'

console.log(rehype)
Output node error-default-import.mjs
SyntaxError: The requested module 'rehype' does not provide an export named 'default'

Cause: rehype has no default export. Fix: use a named import, with braces:

JS
import {rehype} from 'rehype'

The opposite mistake also happens: import {rehypeSanitize} from 'rehype-sanitize' fails because plugins are default exports. Write import rehypeSanitize from 'rehype-sanitize'. When in doubt, the “API” section of each readme states what is exported.

“Cannot use import statement outside a module”

Cause: Node.js is treating your .js file as CommonJS, typically because package.json says "type": "commonjs" (or, on older Node.js versions, says nothing). Fix: rename the file to .mjs, or set "type": "module" in package.json (npm pkg set type=module).

Recent Node.js versions detect import syntax when package.json has no "type" field and run the file as a module anyway, printing a MODULE_TYPELESS_PACKAGE_JSON warning. The same fix silences the warning and avoids the extra parsing work.

ERR_REQUIRE_ESM when using require()

Cause: rehype and its plugins are ES modules only. Older Node.js versions cannot load ES modules with require(). Node.js enabled require(esm) by default in version 22.12.0 and backported it to 20.19.0; in our test on Node.js 22, require('rehype') returned the module without error.

Fix: upgrade Node.js, switch your code to import, or use a dynamic import from CommonJS, which works in all supported versions:

dynamic-import.cjs
// CommonJS file: dynamic import() works on every supported Node.js version.
async function main() {
  const {rehype} = await import('rehype')
  const {default: rehypeSlug} = await import('rehype-slug')
  const file = await rehype()
    .data('settings', {fragment: true})
    .use(rehypeSlug)
    .process('<h2>Hello</h2>')
  console.log(String(file))
}

main()
Output node dynamic-import.cjs
<h2 id="hello">Hello</h2>

If you require() a plugin on a recent Node.js, its default export is on .default: require('rehype-slug').default (we checked that this is a function on Node.js 22).

Output is “[object Promise]”

error-process-sync.mjs
import {rehype} from 'rehype'

// .process() returns a promise. Forgetting await is a common mistake:
const pending = rehype().process('<p>hi</p>')
console.log('without await:', String(pending))

// Awaiting gives you the VFile with the result:
const file = await pending
console.log('with await:', String(file))
Output node error-process-sync.mjs
without await: [object Promise]
with await: <html><head></head><body><p>hi</p></body></html>

Cause: .process() is asynchronous and returns a promise. Fix: await it, or use .then(). unified also offers .processSync(), which only works when every plugin is synchronous.

Unexpected <html>, <head> and <body> in the output

The previous example also shows this: <p>hi</p> came back wrapped in a document. Cause: rehype parses input as a full document by default. Fix: parse as a fragment with rehype().data('settings', {fragment: true}), or .use(rehypeParse, {fragment: true}) when you build the processor yourself. See working with fragments.

My classes, ids or styles disappeared

Cause: almost always rehype-sanitize. The default schema removes style attributes and most class values, and prefixes id and name with user-content-. Fix: sanitize before the plugins that add trusted markup, or extend the schema. The sanitizing tutorial demonstrates both.

A plugin seems to do nothing

  • Missing prerequisites. rehype-autolink-headings only links headings that have ids; add rehype-slug before it.
  • Wrong class names. rehype-highlight only highlights code with a language-* or lang-* class, unless you enable detect.
  • Raw HTML from Markdown. HTML written inside Markdown stays a string that plugins cannot see. Add rehype-raw.
  • Order. A later plugin may undo an earlier one (for example sanitize removing highlight classes). Print the output after each step to find which one.
  • Forgotten call. .use(rehypeSlug) passes the plugin; .use(rehypeSlug()) is wrong because it calls the plugin yourself.

Entities look different: &#x26; instead of &amp;

rehype-stringify chooses how to encode characters. &#x26; and &amp; both mean “&”, so browsers render them identically. If you need a specific style, look at the characterReferences option of rehype-stringify.

“Cannot find package 'rehype-…'”

Cause: the plugin is not installed in this project. Every plugin is its own npm package: npm install rehype-slug. Installing rehype does not install any plugins.

Still stuck?

Reduce the problem to the smallest input and pipeline that shows it; the playground helps for the plugins it supports. For bugs in rehype itself, follow the official support information (opens in a new tab).

Sources and further reading

Use ↑ ↓ to move, Enter to open.