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
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
RewirePlugin
✓ const RewirePlugin = require('rewire-webpack');
✗ import RewirePlugin from 'rewire-webpack';
Package is CommonJS only; ESM import not available.
rewire
✓ const rewire = require('rewire');
✗ import rewire from 'rewire';
rewire package itself is also CommonJS; must be required.
__set__
✓ const myModule = rewire('./myModule'); myModule.__set__('variable', value);
✗ myModule.__set__({ variable: value });
__set__ expects two arguments (key, value), not an object.
__get__
✓ const myModule = rewire('./myModule'); const myVar = myModule.__get__('myVar');
✗ const myVar = myModule.myVar;
__get__ is a method that retrieves private variables; direct access won't work.
__with__
✓ myModule.__with__({ variable: mockValue })(() => { ... });
✗ myModule.__with__('variable', mockValue);
__with__ accepts an object of overrides and returns a function to execute.
Shows how to configure webpack with RewirePlugin and use rewire to get/set private variables.
// 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'
Errors
Common errors & fixes
Cannot find module 'rewire-webpack'
Package not installed or incorrectly required in webpack config.
fixRun `npm install rewire-webpack --save-dev` and use `var RewirePlugin = require('rewire-webpack');` TypeError: myModule.__set__ is not a function
RewirePlugin not added to webpack plugins, or module not wired correctly.
fixAdd `new RewirePlugin()` to webpack's plugins array, and ensure you are using `require('rewire')` before calling `rewire('./module')`. Uncaught ReferenceError: __webpack_require__ is not defined
Using rewire in a non-webpack environment (e.g., Node.js without bundling).
fixRewire-webpack is for browser bundles; use plain `require('rewire')` in Node.js tests. Audit
Dependencies
rewirerequiredCore dependency that provides the dependency injection functionality
webpackrequiredPeer dependency; required to use the plugin