rewire-webpack
raw JSON → 1.0.1 verified Sat Apr 25 auth: no javascript maintenance
A webpack plugin that enables rewire.js dependency injection for client-side bundles generated by webpack. Currently at version 1.0.1, stable. It allows you to mock and inspect modules within webpack bundles for unit testing. Differentiator: integrates rewire's `__set__`, `__get__`, and `__with__` methods into the browser context, which is not natively possible. Works with webpack 1.x; no recent updates since 2015.
Common errors
error Cannot find module 'rewire-webpack' ↓
cause Package not installed or incorrectly required in webpack config.
fix
Run
npm install rewire-webpack --save-dev and use var RewirePlugin = require('rewire-webpack'); error TypeError: myModule.__set__ is not a function ↓
cause RewirePlugin not added to webpack plugins, or module not wired correctly.
fix
Add
new RewirePlugin() to webpack's plugins array, and ensure you are using require('rewire') before calling rewire('./module'). error Uncaught ReferenceError: __webpack_require__ is not defined ↓
cause Using rewire in a non-webpack environment (e.g., Node.js without bundling).
fix
Rewire-webpack is for browser bundles; use plain
require('rewire') in Node.js tests. Warnings
gotcha RewirePlugin only works with webpack 1.x; not compatible with webpack 2+ (breaking changes in plugin API). ↓
fix Use an alternative like babel-plugin-rewire or upgrade to webpack 2 with a shim.
gotcha Do not use `__set__({ key: value })`; it must be two separate arguments. ↓
fix Use `__set__('key', value)` instead of an object.
gotcha Cannot rewire modules that use ES module syntax (import/export) without additional Babel transforms. ↓
fix Use rewire's Babel plugin or transpile to CommonJS before bundling.
deprecated Package is unmaintained; last release in 2015. ↓
fix Consider using rewiremock or babel-plugin-rewire for modern projects.
gotcha The plugin may cause issues with webpack's hot module replacement (HMR). ↓
fix Disable HMR when using RewirePlugin, or only use for test builds.
Install
npm install rewire-webpack yarn add rewire-webpack pnpm add rewire-webpack Imports
- RewirePlugin wrong
import RewirePlugin from 'rewire-webpack';correctconst RewirePlugin = require('rewire-webpack'); - rewire wrong
import rewire from 'rewire';correctconst rewire = require('rewire'); - __set__ wrong
myModule.__set__({ variable: value });correctconst myModule = rewire('./myModule'); myModule.__set__('variable', value); - __get__ wrong
const myVar = myModule.myVar;correctconst myModule = rewire('./myModule'); const myVar = myModule.__get__('myVar'); - __with__ wrong
myModule.__with__('variable', mockValue);correctmyModule.__with__({ variable: mockValue })(() => { ... });
Quickstart
// webpack.config.js
var RewirePlugin = require('rewire-webpack');
module.exports = {
entry: './index.js',
output: { path: __dirname + '/dist', filename: 'bundle.js' },
plugins: [
new RewirePlugin()
]
};
// src/module.js
var privateVar = 'secret';
function getPrivate() { return privateVar; }
module.exports = { getPrivate: getPrivate };
// test.js (browser bundle)
var rewire = require('rewire');
var myModule = rewire('./module.js');
console.log(myModule.__get__('privateVar')); // 'secret'
myModule.__set__('privateVar', 'mocked');
console.log(myModule.getPrivate()); // 'mocked'