{"id":19158,"library":"babel-plugin-rewire-exports","title":"babel-plugin-rewire-exports","description":"Babel plugin for stubbing ES6/ES2015 module exports in tests. Version 2.3.0 (stable, last release 2021) allows rewriting exported values in importing modules without modifying module internals. Unlike babel-plugin-rewire, it keeps imports and local variables intact. It generates rewire/stub functions (rewire for default, rewire$name for named exports, and restore). Works with Babel 7 (use 0.x for Babel 6), supports browser and Node ESM, and bundles with Webpack, Rollup, SystemJS, and CommonJS via env preset. Release cadence: infrequent, no recent updates.","status":"active","version":"2.3.0","language":"javascript","source_language":"en","source_url":"https://github.com/asapach/babel-plugin-rewire-exports","tags":["javascript","babel","plugin","rewire","babel-plugin","esm","unit-testing","webpack","rollup"],"install":[{"cmd":"npm install babel-plugin-rewire-exports","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-rewire-exports","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-rewire-exports","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Requires Babel 7 core as peer dependency.","package":"@babel/core","optional":false}],"imports":[{"note":"The plugin exports rewire as a named export, not default. It replaces the default export of the module.","wrong":"import rewire from './my-module'","symbol":"rewire (default export stub)","correct":"import { rewire } from './my-module'"},{"note":"For named exports, the stub is rewire$<exportName>. The dollar sign is part of the identifier.","wrong":"import { rewire_name } from './my-module'","symbol":"rewire$name (named export stub)","correct":"import { rewire$name } from './my-module'"},{"note":"restore is a named export from the transformed module, not default.","wrong":"import restore from './my-module'","symbol":"restore","correct":"import { restore } from './my-module'"}],"quickstart":{"code":"// .babelrc\n{\n  \"plugins\": [\"babel-plugin-rewire-exports\"]\n}\n\n// text.js\nexport let message = 'Hello world!';\n\n// logger.js\nimport {message} from './text.js';\nexport default function log() {\n  console.log(message);\n}\n\n// test.js\nimport {rewire$message, restore} from './text.js';\nimport log from './logger.js';\n\nlog(); // 'Hello world!'\nrewire$message('stubbed');\nlog(); // 'stubbed'\nrestore();\nlog(); // 'Hello world!'","lang":"javascript","description":"Shows basic setup and usage: rewire a named export, restore original."},"warnings":[{"fix":"In tests, access the function via the variable export, not the original function declaration. Use rewire$default for default exports.","message":"v2.0.0: Functions are rewritten as variables. Export default function foo() {} becomes var foo = function foo() {}; export { foo as default };","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"If relying on rewiring const inside the module, use unsafeConst option or upgrade to >=1.3.0 and export as let/var.","message":"v1.3.0: Constant exports behavior changed. Constants can't be rewired within the module; only the exported binding is mutable.","severity":"deprecated","affected_versions":"<1.3.0 >=1.0.0"},{"fix":"In .babelrc, order: [\"babel-plugin-rewire-exports\", \"@babel/plugin-transform-modules-commonjs\"]","message":"Plugin must be placed before @babel/plugin-transform-modules-commonjs when targeting CommonJS.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use rewire(stub) for default exports, not named import default.","message":"Default export rewiring: rewire() stub replaces the default export, but the original default is kept and can be restored via restore().","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Check for name collisions and use the renamed stubs accordingly.","message":"If the module already has top-level identifiers named rewire or restore, the generated stubs are renamed to rewire$default and restore$rewire.","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 --save-dev @babel/core babel-plugin-rewire-exports","cause":"Missing @babel/core peer dependency.","error":"Error: Plugin babel-plugin-rewire-exports requires @babel/core but was not found."},{"fix":"Add \"unsafeConst\": true to plugin options, or change export to let/var.","cause":"Attempting to rewire a constant export without unsafeConst option.","error":"TypeError: Cannot assign to read only property 'message' of object '#<Object>'"},{"fix":"Upgrade Node or use a bundler that supports the plugin's output.","cause":"Using node <8.5.0 or browser without ESM support for rewire$name identifier.","error":"SyntaxError: Unexpected token $"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}