module-replace-webpack-plugin
raw JSON → 0.0.12 verified Sat Apr 25 auth: no javascript abandoned
A webpack plugin (v0.0.12, latest as of 2025) that replaces imported modules at build time, allowing monkey patching of third-party libraries without modifying source code. Designed for webpack 3.x only. Uses regex-based matching to swap `require`/`import` statements. No updates since 2018; limited compatibility and no support for webpack 4+ or ESM. Different from string-replace-loader as it operates on module resolution rather than string substitution.
Common errors
error Error: ModuleReplaceWebpackPlugin is not a constructor ↓
cause Using ES module import instead of CommonJS require.
fix
Use const ModuleReplaceWebpackPlugin = require('module-replace-webpack-plugin');
error TypeError: Cannot read property 'push' of undefined ↓
cause options.modules is not an array or is missing.
fix
Ensure options contains modules: [{ test: /.../, replace: '...' }]
Warnings
deprecated Plugin only supports webpack 3.x; may break on webpack 4+. ↓
fix Upgrade to a modern alternative like string-replace-loader or NormalModuleReplacementPlugin.
gotcha The 'replace' path must be relative to root; does not automatically resolve from entry directory. ↓
fix Use path.resolve(__dirname, 'src/patched.js') to specify absolute path.
gotcha Multiple module rules are matched first-come-first-serve; overlapping regexes may cause unexpected replacement. ↓
fix Order specific regexes before generic ones in the modules array.
gotcha File extension in 'replace' is mandatory; omitting it will not work. ↓
fix Always include file extension, e.g., './patched.js' not './patched'.
Install
npm install module-replace-webpack-plugin yarn add module-replace-webpack-plugin pnpm add module-replace-webpack-plugin Imports
- ModuleReplaceWebpackPlugin wrong
import ModuleReplaceWebpackPlugin from 'module-replace-webpack-plugin';correctconst ModuleReplaceWebpackPlugin = require('module-replace-webpack-plugin'); - new ModuleReplaceWebpackPlugin(options) wrong
new ModuleReplaceWebpackPlugin({ modules: { test: /lodash/, replace: './patched.js' } })correctnew ModuleReplaceWebpackPlugin({ modules: [{ test: /lodash/, replace: './patched.js' }] }) - exclude wrong
new ModuleReplaceWebpackPlugin({ modules: [...], exclude: ['patched.js'] })correctnew ModuleReplaceWebpackPlugin({ modules: [...], exclude: [/patched\.js$/] })
Quickstart
const ModuleReplaceWebpackPlugin = require('module-replace-webpack-plugin');
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new ModuleReplaceWebpackPlugin({
modules: [{
test: /lodash/,
replace: './src/patchedLodash.js'
}],
exclude: [/patchedLodash\.js$/]
})
]
};