{"id":19160,"library":"babel-plugin-rewire","title":"babel-plugin-rewire","description":"A Babel plugin that adds __Rewire__, __ResetDependency__, and __GetDependency__ methods to modules for mocking dependencies in tests. Version 1.2.0 is stable with moderate release cadence. It supports ES6 import, CommonJS require(), and top-level variable/function rewiring. Unlike proxyquire or rewire.js, it works at the AST level via Babel, enabling rewiring of named exports and internal dependencies. Supports both default and named exports, and automatically handles primitive vs object default exports. Compatible with React and other transpiled environments.","status":"active","version":"1.2.0","language":"javascript","source_language":"en","source_url":"git://github.com/speedskater/babel-plugin-rewire","tags":["javascript","babel","plugin","rewire","es6","modules"],"install":[{"cmd":"npm install babel-plugin-rewire","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-rewire","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-rewire","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as peer dependency to transform code","package":"@babel/core","optional":false}],"imports":[{"note":"Works with both ESM and CJS after plugin transforms code.","wrong":"const myModule = require('my-module'); myModule.__Rewire__('dep', mock)","symbol":"__Rewire__","correct":"import myModule from 'my-module'; myModule.__Rewire__('dep', mock)"},{"note":"Resets a specific dependency to its original value.","wrong":"","symbol":"__ResetDependency__","correct":"myModule.__ResetDependency__('dep')"},{"note":"Gets the current value of a dependency.","wrong":"","symbol":"__GetDependency__","correct":"const dep = myModule.__GetDependency__('dep')"},{"note":"Compatibility alias for rewire.js; returns a revert function since v1.0.0-rc-7.","wrong":"","symbol":"__set__","correct":"myModule.__set__('dep', mock)"},{"note":"Exported as named export and on default export (for non-primitive defaults).","wrong":"","symbol":"__RewireAPI__","correct":"import { __RewireAPI__ } from 'my-module'; __RewireAPI__.__Rewire__('dep', mock)"},{"note":"For primitive default exports, methods are not attached; use __RewireAPI__ instead.","wrong":"import * as mod from 'my-module'; mod.default.__Rewire__('dep', mock)","symbol":"default export rewiring","correct":"import DefaultExport from 'my-module'; DefaultExport.__Rewire__('dep', mock)"}],"quickstart":{"code":"// babel.config.js\nmodule.exports = {\n  plugins: [['babel-plugin-rewire', { 'rewire': { 'exclude': /node_modules/ } }]]\n};\n\n// myModule.js\nexport function greet(name) {\n  return `Hello ${name}`;\n}\n\n// test.js\nimport myModule from './myModule';\nmyModule.__Rewire__('greet', (name) => `Hi ${name}`);\nconst result = myModule.greet('World');\nconsole.log(result); // 'Hi World'\nmyModule.__ResetDependency__('greet');\nconsole.log(myModule.greet('World')); // 'Hello World'","lang":"javascript","description":"Basic configuration and usage of babel-plugin-rewire: define module, rewire function, call, reset."},"warnings":[{"fix":"Install @babel/core: npm install --save-dev @babel/core","message":"Peer dependency @babel/core v7+ required; not compatible with Babel 6.","severity":"breaking","affected_versions":">=1.2.0"},{"fix":"Use __Rewire__('dep', value) and __GetDependency__('dep') instead.","message":"__set__ and __get__ are deprecated in favor of __Rewire__ and __GetDependency__.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Access via import { __RewireAPI__ } from 'module' or use __RewireAPI__.__Rewire__()","message":"Default exports of primitive types (string, number, boolean) do not receive __Rewire__ methods; use __RewireAPI__ instead.","severity":"gotcha","affected_versions":"*"},{"fix":"Only changes the internal reference within the rewired module.","message":"Rewiring a named export does not affect other modules that import that same named export.","severity":"gotcha","affected_versions":"*"},{"fix":"Refactor complex initializations into separate functions or use __Rewire__ on the entire module export if possible.","message":"Top-level variable rewiring works only for variables initialized with require() or literals, not for complex expressions.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure the module is processed by Babel with the plugin. For Node tests, use @babel/register or equivalent.","cause":"Module not transformed by babel-plugin-rewire or imported as CJS before transformation.","error":"__Rewire__ is not a function"},{"fix":"Run: npm install --save-dev babel-plugin-rewire","cause":"Plugin not installed or missing from node_modules.","error":"Cannot find module 'babel-plugin-rewire'"},{"fix":"Use __RewireAPI__ instead or avoid freezing exports.","cause":"Using strict mode or frozen object; plugin cannot attach methods to immutable default exports.","error":"Cannot assign to read only property '__Rewire__' of object '#<Object>'"},{"fix":"Ensure the dependency is imported at module top level.","cause":"Plugin can only rewire dependencies that are statically analyzable in the module source (import/require at top level).","error":"Cannot rewire a dependency that is not explicitly imported in module."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}