Documentation menu
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 withnpm init -yif needed.
Install the package
npm install rehypenpm 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:
npm install --save-dev rehypeEnable ES modules
rehype is ESM only. Either use the .mjs extension for your scripts, or mark the whole project as ESM:
npm pkg set type=moduleThat adds this line to 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:
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))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:
npm install rehype-sanitize rehype-slug rehype-formatPlugins are default exports, and you pass them to .use() without calling them:
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
| Tool | Command |
|---|---|
| npm | npm install rehype |
| pnpm | pnpm add rehype |
| Yarn | yarn add rehype |
| Bun | bun 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.