{"id":19015,"library":"babel-plugin-add-module-exports","title":"babel-plugin-add-module-exports","description":"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.","status":"maintenance","version":"1.0.4","language":"javascript","source_language":"en","source_url":"https://github.com/59naga/babel-plugin-add-module-exports","tags":["javascript","babel-plugin","module.exports"],"install":[{"cmd":"npm install babel-plugin-add-module-exports","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-add-module-exports","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-add-module-exports","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Typically used as a string in Babel config: 'plugins': ['add-module-exports']","wrong":"const addModuleExports = require('babel-plugin-add-module-exports') (only for programmatic use, not config)","symbol":"add-module-exports","correct":"import addModuleExports from 'babel-plugin-add-module-exports' (in .babelrc or Babel config)"},{"note":"Optional option to also set `module.exports.default` for backward compatibility with code using `.default`.","wrong":"Passing options as a separate object without the plugin name array","symbol":"addDefaultProperty","correct":"In .babelrc: { 'plugins': [['add-module-exports', { 'addDefaultProperty': true }]] }"}],"quickstart":{"code":"// Add plugin to Babel config (e.g., .babelrc, babel.config.js).\n// Note: This plugin only works when @babel/preset-env or babel-preset-env is used with CommonJS modules.\n{\n  \"presets\": [\"@babel/preset-env\"],\n  \"plugins\": [\"add-module-exports\"]\n}\n// With options:\n{\n  \"presets\": [\"@babel/preset-env\"],\n  \"plugins\": [\n    [\"add-module-exports\", { \"addDefaultProperty\": true }]\n  ]\n}\n// Example source file (test.js):\nexport default function sayHello() {\n  return 'Hello';\n}\n// After transformation:\nObject.defineProperty(exports, '__esModule', { value: true });\nexports.default = sayHello;\nmodule.exports = exports['default']; // added by this plugin\n// Now require('./test') returns the function directly, not an object with .default.","lang":"javascript","description":"Shows Babel configuration and expected transformation of a default export to also set module.exports."},"warnings":[{"fix":"Set `modules` option in preset-env to 'commonjs' or 'umd'.","message":"Only works when @babel/preset-env (or env) is used with modules: 'commonjs' or 'umd'. Does nothing with modules: false, amd, or systemjs.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If you have named exports, this plugin will not change module.exports. Use other approaches or restructure exports.","message":"Does not assign 'module.exports' if there are named exports in addition to the default export. Only triggers on a sole default export.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Switch to using `@babel/plugin-transform-modules-commonjs` with `loose: true` which achieves similar behavior.","message":"Plugin is in maintenance mode with no active development. Consider alternatives like setting `@babel/plugin-transform-modules-commonjs` loose mode.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Ensure the default export is not an object with its own 'default' property, or avoid addDefaultProperty.","message":"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.","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 babel-plugin-add-module-exports --save-dev` or `yarn add -D babel-plugin-add-module-exports`.","cause":"Plugin not installed or not listed as a devDependency.","error":"Error: Cannot find module 'babel-plugin-add-module-exports'"},{"fix":"Ensure you are using Babel 6 or 7. For Babel 7, use preset-env as @babel/preset-env.","cause":"Babel version mismatch (e.g., Babel 7 vs 6). This plugin works with Babel 6 and 7.","error":"Module build failed: Error: [BABEL] unknown plugin \"add-module-exports\""},{"fix":"Set `modules: 'commonjs'` in @babel/preset-env options.","cause":"Default export is not assigned to module.exports because the plugin requires CommonJS modules to be enabled.","error":"TypeError: Cannot read property 'default' of undefined"},{"fix":"Ensure the file has a single `export default` statement.","cause":"The source file uses named exports or no default export.","error":"Warning: add-module-exports plugin only works when exporting using `export default`."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}