babel-plugin-add-module-exports

raw JSON →
1.0.4 verified Sat Apr 25 auth: no javascript maintenance

This Babel plugin restores the pre-Babel 6 behavior where a default export is also assigned to `module.exports`, allowing `require()` to return the default value directly without the `.default` property. Version 1.0.4 is the latest stable release, with no active development. It works only with CommonJS/UMD module output (requires `modules` to be set to `'commonjs'` or `'umd'`). Unlike alternatives, it only adds `module.exports` when there is a single default export, preserving named exports.

error Error: Cannot find module 'babel-plugin-add-module-exports'
cause Plugin not installed or not listed as a devDependency.
fix
Run npm install babel-plugin-add-module-exports --save-dev or yarn add -D babel-plugin-add-module-exports.
error Module build failed: Error: [BABEL] unknown plugin "add-module-exports"
cause Babel version mismatch (e.g., Babel 7 vs 6). This plugin works with Babel 6 and 7.
fix
Ensure you are using Babel 6 or 7. For Babel 7, use preset-env as @babel/preset-env.
error TypeError: Cannot read property 'default' of undefined
cause Default export is not assigned to module.exports because the plugin requires CommonJS modules to be enabled.
fix
Set modules: 'commonjs' in @babel/preset-env options.
error Warning: add-module-exports plugin only works when exporting using `export default`.
cause The source file uses named exports or no default export.
fix
Ensure the file has a single export default statement.
gotcha Only works when @babel/preset-env (or env) is used with modules: 'commonjs' or 'umd'. Does nothing with modules: false, amd, or systemjs.
fix Set `modules` option in preset-env to 'commonjs' or 'umd'.
gotcha Does not assign 'module.exports' if there are named exports in addition to the default export. Only triggers on a sole default export.
fix If you have named exports, this plugin will not change module.exports. Use other approaches or restructure exports.
deprecated Plugin is in maintenance mode with no active development. Consider alternatives like setting `@babel/plugin-transform-modules-commonjs` loose mode.
fix Switch to using `@babel/plugin-transform-modules-commonjs` with `loose: true` which achieves similar behavior.
gotcha If used with `addDefaultProperty: true`, the default export is both assigned to `module.exports` and also kept as `module.exports.default`. This may cause unexpected behavior if the default export is an object with a 'default' key.
fix Ensure the default export is not an object with its own 'default' property, or avoid addDefaultProperty.
npm install babel-plugin-add-module-exports
yarn add babel-plugin-add-module-exports
pnpm add babel-plugin-add-module-exports

Shows Babel configuration and expected transformation of a default export to also set module.exports.

// Add plugin to Babel config (e.g., .babelrc, babel.config.js).
// Note: This plugin only works when @babel/preset-env or babel-preset-env is used with CommonJS modules.
{
  "presets": ["@babel/preset-env"],
  "plugins": ["add-module-exports"]
}
// With options:
{
  "presets": ["@babel/preset-env"],
  "plugins": [
    ["add-module-exports", { "addDefaultProperty": true }]
  ]
}
// Example source file (test.js):
export default function sayHello() {
  return 'Hello';
}
// After transformation:
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = sayHello;
module.exports = exports['default']; // added by this plugin
// Now require('./test') returns the function directly, not an object with .default.