Registry / devops / rollup-plugin-regexp

rollup-plugin-regexp

JSON →
library5.0.1jsnpmunverified

A Rollup plugin for dynamic find-and-replace operations in bundled output using regular expressions or string matching. Version 5.0.1 is the latest stable release, published with a moderate cadence. Unlike static string replacement plugins, it supports RegExp patterns and replacement functions, enabling codemods and dynamic content injection. Ships TypeScript types and uses magic-string for accurate sourcemap preservation. A key differentiator is the shorthand syntax allowing key-value pairs for multiple replacements.

npm install rollup-plugin-regexp
INSTALL
IMPORT
SIG · ROLLUP-PLUGIN-REGE
R
rollup-plugin-regexp
devopsjavascriptv5.0.1
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 18223 runs
build_error
glibc
node 18223 runs
build_error
Code
Verified usage

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

default (rollup-plugin-regexp)
import regexpPlugin from 'rollup-plugin-regexp'
const regexpPlugin = require('rollup-plugin-regexp')
Package is ESM-only; no CommonJS export. Use dynamic import if needed in CJS context.
RegexpPlugin (named export)
import { RegexpPlugin } from 'rollup-plugin-regexp'
import RegexpPlugin from 'rollup-plugin-regexp'
Named export is available but default export is more commonly used. Both work identically.
TypeScript types
import type { RollupRegexpOptions } from 'rollup-plugin-regexp'
Types are exported from the package, so no separate @types/ package is needed.

Shows default import, a regex replacement using a function, and the shorthand multiple replacement syntax.

import regexpPlugin from 'rollup-plugin-regexp'; import { defineConfig } from 'rollup'; export default defineConfig({ input: 'src/index.js', output: { dir: 'dist', format: 'esm' }, plugins: [ regexpPlugin({ find: /process\.env\.MY_VAR/g, replace: () => JSON.stringify(process.env.MY_VAR ?? 'default') }), // Multiple replacements shorthand: regexpPlugin({ '__BUILD_TIME__': new Date().toISOString(), '__VERSION__': '1.0.0' }) ] });
Debug
Known issues
breakingVersion 5.0.0 dropped support for CommonJS (require). If using Node.js <12 or bundling for CommonJS, pin to version 4.x.
fix
Use import syntax or downgrade to 'rollup-plugin-regexp@4'
affects: >=5.0.0
breakingPrior to v5, package was named 'rollup-plugin-modify'. The package was renamed; old imports break.
fix
Update imports from 'rollup-plugin-modify' to 'rollup-plugin-regexp'
affects: >=5.0.0
gotchaReplacement function receives the match array from String.prototype.replace(). If using a global regex, the function is called multiple times but the 'offset' parameter may be off if replacements change string length.
fix
Ensure replacement function correctly handles all parameters: match, p1, p2, ..., offset, string.
affects: >=1.0.0
deprecatedThe 'include' and 'exclude' options (from @rollup/pluginutils) were deprecated in v5 in favor of Rollup's built-in filter plugins like 'rollup-plugin-filter'.
fix
Use external filter plugins or omit; the plugin only transforms matched files.
affects: >=5.0.0
gotchaWhen using multiple instances with overlapping patterns, execution order matters and may cause unintended replacements if one instance modifies the same code as another.
fix
Order plugins carefully or consolidate replacements into a single instance.
affects: >=1.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module [...] not supported.
Attempting to require() an ESM-only package in a CommonJS module.
fix
Use dynamic import: const regexpPlugin = await import('rollup-plugin-regexp'); or use import syntax. Alternatively, downgrade to v4.
TypeError: regexpPlugin is not a function
Wrong import style – likely using default import when package was required as an object.
fix
Ensure import is default: import regexpPlugin from 'rollup-plugin-regexp' (no curly braces).
Cannot read properties of undefined (reading 'replace')
Passing an undefined or non-string value as 'find' or 'replace' option.
fix
Check that 'find' is a string or RegExp, and 'replace' is a string or function.
The 'regexpPlugin' option 'find' must be a string or RegExp.
Passing an object or boolean to 'find'.
fix
Ensure 'find' is a string or RegExp. If using shorthand syntax, keys must be strings.
Upgrade
Version history
5.0.1latest on npm
Audit
Dependencies
@rollup/pluginutilsrequiredUsed internally for plugin utilities like creating a filter from include/exclude patterns
magic-stringrequiredHandles source transformations while preserving sourcemaps
Agent activity
2 hits · last 30 days
node
2
Resources
rollup-plugin-regexp — npm install rollup-plugin-regexp · libregistry