Install rehype with npm

The rehype package on npm is the usual way to use rehype. It bundles unified, rehype-parse and rehype-stringify, so one install gives you a processor that reads and writes HTML.

Before you start

  • Node.js and npm. Run node --version. The rehype readme says the current major version, rehype@^13, is kept compatible with Node.js 16 and later, while noting that unified projects follow maintained Node.js versions. Use a maintained LTS release.
  • A project folder with a package.json. Create one with npm init -y if needed.

Install the package

Terminal
npm install rehype

npm downloads rehype and its dependencies into node_modules and records it in package.json under dependencies. If rehype is only used by build scripts, you can add it as a development dependency instead:

Terminal
npm install --save-dev rehype

Enable ES modules

rehype is ESM only. Either use the .mjs extension for your scripts, or mark the whole project as ESM:

Terminal
npm pkg set type=module

That adds this line to package.json:

package.json
{
  "type": "module"
}

Verify the installation

Save the following as check.mjs and run node check.mjs. It parses a fragment and prints it back, which proves the parser and serializer work:

fragment-vs-document.mjs
import {rehype} from 'rehype'

const input = '<p>Just a paragraph</p>'

const asDocument = await rehype().process(input)
const asFragment = await rehype().data('settings', {fragment: true}).process(input)

console.log('document:', String(asDocument))
console.log('fragment:', String(asFragment))
Output node fragment-vs-document.mjs
document: <html><head></head><body><p>Just a paragraph</p></body></html>
fragment: <p>Just a paragraph</p>

You can also confirm the installed version with npm ls rehype. For the newest published version, check the npm page (opens in a new tab) or run npm view rehype version.

Add plugins

Each plugin is a separate package. Install the ones you need alongside rehype:

Terminal
npm install rehype-sanitize rehype-slug rehype-format

Plugins are default exports, and you pass them to .use() without calling them:

JS
import {rehype} from 'rehype'
import rehypeSanitize from 'rehype-sanitize'
import rehypeSlug from 'rehype-slug'
import rehypeFormat from 'rehype-format'

const file = await rehype()
  .use(rehypeSanitize)
  .use(rehypeSlug)
  .use(rehypeFormat)
  .process('<h1>Hello</h1>')

See the plugin library for details on each plugin, or the Plugin Explorer to generate an install command.

Other package managers

ToolCommand
npmnpm install rehype
pnpmpnpm add rehype
Yarnyarn add rehype
Bunbun add rehype

All of them install the same package from the npm registry.

Use rehype without npm (Deno and browsers)

The rehype readme also documents loading it from the esm.sh CDN in Deno and in browsers, for example import {rehype} from 'https://esm.sh/rehype@13'. For production websites, bundling the package yourself is usually the better choice; this site's tools are built that way.

Next steps

Continue with getting started, or install the command line interface to work with files.

Sources and further reading

Use ↑ ↓ to move, Enter to open.