{"id":20519,"library":"rewire-webpack","title":"rewire-webpack","description":"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.","status":"maintenance","version":"1.0.1","language":"javascript","source_language":"en","source_url":"git://github.com/jhnns/rewire-webpack","tags":["javascript","dependency","injection","mock","unit","test","leak","webpack","browser"],"install":[{"cmd":"npm install rewire-webpack","lang":"bash","label":"npm"},{"cmd":"yarn add rewire-webpack","lang":"bash","label":"yarn"},{"cmd":"pnpm add rewire-webpack","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core dependency that provides the dependency injection functionality","package":"rewire","optional":false},{"reason":"Peer dependency; required to use the plugin","package":"webpack","optional":false}],"imports":[{"note":"Package is CommonJS only; ESM import not available.","wrong":"import RewirePlugin from 'rewire-webpack';","symbol":"RewirePlugin","correct":"const RewirePlugin = require('rewire-webpack');"},{"note":"rewire package itself is also CommonJS; must be required.","wrong":"import rewire from 'rewire';","symbol":"rewire","correct":"const rewire = require('rewire');"},{"note":"__set__ expects two arguments (key, value), not an object.","wrong":"myModule.__set__({ variable: value });","symbol":"__set__","correct":"const myModule = rewire('./myModule'); myModule.__set__('variable', value);"},{"note":"__get__ is a method that retrieves private variables; direct access won't work.","wrong":"const myVar = myModule.myVar;","symbol":"__get__","correct":"const myModule = rewire('./myModule'); const myVar = myModule.__get__('myVar');"},{"note":"__with__ accepts an object of overrides and returns a function to execute.","wrong":"myModule.__with__('variable', mockValue);","symbol":"__with__","correct":"myModule.__with__({ variable: mockValue })(() => { ... });"}],"quickstart":{"code":"// webpack.config.js\nvar RewirePlugin = require('rewire-webpack');\nmodule.exports = {\n  entry: './index.js',\n  output: { path: __dirname + '/dist', filename: 'bundle.js' },\n  plugins: [\n    new RewirePlugin()\n  ]\n};\n\n// src/module.js\nvar privateVar = 'secret';\nfunction getPrivate() { return privateVar; }\nmodule.exports = { getPrivate: getPrivate };\n\n// test.js (browser bundle)\nvar rewire = require('rewire');\nvar myModule = rewire('./module.js');\nconsole.log(myModule.__get__('privateVar')); // 'secret'\nmyModule.__set__('privateVar', 'mocked');\nconsole.log(myModule.getPrivate()); // 'mocked'","lang":"javascript","description":"Shows how to configure webpack with RewirePlugin and use rewire to get/set private variables."},"warnings":[{"fix":"Use an alternative like babel-plugin-rewire or upgrade to webpack 2 with a shim.","message":"RewirePlugin only works with webpack 1.x; not compatible with webpack 2+ (breaking changes in plugin API).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use `__set__('key', value)` instead of an object.","message":"Do not use `__set__({ key: value })`; it must be two separate arguments.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use rewire's Babel plugin or transpile to CommonJS before bundling.","message":"Cannot rewire modules that use ES module syntax (import/export) without additional Babel transforms.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Consider using rewiremock or babel-plugin-rewire for modern projects.","message":"Package is unmaintained; last release in 2015.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Disable HMR when using RewirePlugin, or only use for test builds.","message":"The plugin may cause issues with webpack's hot module replacement (HMR).","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run `npm install rewire-webpack --save-dev` and use `var RewirePlugin = require('rewire-webpack');`","cause":"Package not installed or incorrectly required in webpack config.","error":"Cannot find module 'rewire-webpack'"},{"fix":"Add `new RewirePlugin()` to webpack's plugins array, and ensure you are using `require('rewire')` before calling `rewire('./module')`.","cause":"RewirePlugin not added to webpack plugins, or module not wired correctly.","error":"TypeError: myModule.__set__ is not a function"},{"fix":"Rewire-webpack is for browser bundles; use plain `require('rewire')` in Node.js tests.","cause":"Using rewire in a non-webpack environment (e.g., Node.js without bundling).","error":"Uncaught ReferenceError: __webpack_require__ is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}