Registry / testing / rewire-webpack

rewire-webpack

JSON →
library1.0.1jsnpmunverified

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.

npm install rewire-webpack
INSTALL
IMPORT
SIG · REWIRE-WEBPACK
R
rewire-webpack
testingjavascriptv1.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 18226 runs
build_error
glibc
node 18226 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'
Debug
Known issues
gotchaRewirePlugin 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.
affects: >=1.0.0
gotchaDo not use `__set__({ key: value })`; it must be two separate arguments.
fix
Use `__set__('key', value)` instead of an object.
affects: >=1.0.0
gotchaCannot 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.
affects: >=1.0.0
deprecatedPackage is unmaintained; last release in 2015.
fix
Consider using rewiremock or babel-plugin-rewire for modern projects.
affects: >=1.0.0
gotchaThe plugin may cause issues with webpack's hot module replacement (HMR).
fix
Disable HMR when using RewirePlugin, or only use for test builds.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'rewire-webpack'
Package not installed or incorrectly required in webpack config.
fix
Run `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.
fix
Add `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).
fix
Rewire-webpack is for browser bundles; use plain `require('rewire')` in Node.js tests.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
rewirerequiredCore dependency that provides the dependency injection functionality
webpackrequiredPeer dependency; required to use the plugin
Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
rewire-webpack — npm install rewire-webpack · libregistry