{"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,"search_vec":"'0':63 '2.3.0':16 '2021':20 '6':67 '7':61 'allow':21 'babel':2,6,34,60,66,91,95 'babel-plugin':94 'babel-plugin-rewir':33 'babel-plugin-rewire-export':1 'browser':69 'bundl':74 'cadenc':85 'commonj':80 'default':50 'env':82 'es6/es2015':10 'esm':72,97 'export':5,12,23,55 'function':47 'generat':45 'import':26,39 'infrequ':86 'intact':43 'intern':31 'javascript':90 'keep':38 'last':18 'local':41 'modifi':29 'modul':11,27,30 'name':52,54 'node':71 'plugin':3,7,35,92,96 'preset':83 'recent':88 'releas':19,84 'restor':57 'rewir':4,36,48,51,93 'rewire/stub':46 'rewrit':22 'rollup':77,102 'stabl':17 'stub':9 'support':68 'systemj':78 'test':14,100 'unit':99 'unit-test':98 'unlik':32 'updat':89 'use':62 'valu':24 'variabl':42 'version':15 'via':81 'webpack':76,101 'without':28 'work':58 'x':64","created_at":"2026-04-25T11:16:20.983139+00:00","updated_at":"2026-04-25T11:16:20.983139+00:00","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,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/asapach/babel-plugin-rewire-exports","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/babel-plugin-rewire-exports","openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-24","install_tag":null}}