Registry / devops / rollup-plugin-module-replacement

rollup-plugin-module-replacement

JSON →
library1.2.1jsnpmunverified

A Rollup plugin (v1.2.1) that replaces module paths during bundling, similar to Webpack's NormalModuleReplacementPlugin. Supports string or regex pattern matching with replacement strings or functions for dynamic rewrites. Ideal for environment-specific builds (e.g., swapping mock vs production modules). Unlike rollup-plugin-alias, it does not resolve modules internally, allowing custom resolvers (e.g., rollup-plugin-node-resolve) to be passed in. Released under MIT, with irregular updates (last in 2020).

npm install rollup-plugin-module-replacement
INSTALL
IMPORT
SIG · ROLLUP-PLUGIN-MODU
R
rollup-plugin-module-replacement
devopsjavascriptv1.2.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
import replacement from 'rollup-plugin-module-replacement'
const replacement = require('rollup-plugin-module-replacement')
Plugin is ESM-only; CJS require will fail in Rollup ES configs.
replacement
import replacement from 'rollup-plugin-module-replacement'
import { replacement } from 'rollup-plugin-module-replacement'
No named export; must use default import.
replacement as named
import replacer from 'rollup-plugin-module-replacement'
import { replaceModules } from 'rollup-plugin-module-replacement'
Only default export; no alias export exists.

Demonstrates regex-based module replacement with a custom resolver using rollup-plugin-node-resolve.

// rollup.config.js import replacement from 'rollup-plugin-module-replacement'; import resolve from 'rollup-plugin-node-resolve'; const customResolver = resolve({ extensions: ['.mjs', '.js', '.jsx'] }); export default { input: 'src/index.js', output: { file: 'dist/bundle.js', format: 'cjs' }, plugins: [ replacement( { entries: [ { find: /src\/(.*)/, replacement: (importee) => { const env = process.env.BUILD_ENV || 'prod'; return importee.replace('src', `src-${env}`); } } ] }, customResolver ), resolve() ] };
Debug
Known issues
gotchaPlugin does not resolve replaced paths internally; if no customResolver is passed, the replacement string is used as-is in Rollup's resolve hook, which may lead to unresolved modules.
fix
Pass a resolver plugin (e.g., rollup-plugin-node-resolve) as the second argument or ensure replacement results in an absolute path.
affects: >=1.0.0
gotchaThe order of entries matters: first matching rule is applied. Subsequent rules are not processed for the same import.
fix
Order entries from most specific to least specific to avoid unintended matches.
affects: >=1.0.0
deprecatedrollup-plugin-node-resolve is now @rollup/plugin-node-resolve.
fix
Replace imports from 'rollup-plugin-node-resolve' to '@rollup/plugin-node-resolve'.
affects: >=1.2.0
gotchaReplacement function must return a string; returning undefined or null will cause Rollup to fail resolving module.
fix
Always return a string from the replacement function. For unmatched patterns, return the original importee.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Could not resolve './some-module' from src/index.js
Module replacement did not map to an existing file, or no customResolver handled the replaced path.
fix
Ensure replacement produces a valid path, or pass a customResolver as second argument to the plugin.
TypeError: replacement is not a function
Incorrect import: using named import instead of default import.
fix
Use: import replacement from 'rollup-plugin-module-replacement'
The plugin 'rollup-plugin-module-replacement' does not support the 'transform' hook.
Plugin only uses resolveId hook; trying to use it for code transformation will fail.
fix
Use this plugin only for module path replacement, not for content transformation.
Upgrade
Version history
1.2.1latest on npm
Audit
Dependencies
rolluprequiredPeer dependency required for plugin compatibility.
Agent activity
4 hits · last 30 days
node
4
Resources
rollup-plugin-module-replacement — npm install rollup-plugin-module-replacement · libregistry