Registry / web-framework / babel-plugin-add-module-exports

babel-plugin-add-module-exports

JSON →
library1.0.4jsnpmunverified

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.

npm install babel-plugin-add-module-exports
INSTALL
IMPORT
SIG · BABEL-PLUGIN-ADD-M
B
babel-plugin-add-module-exports
web-frameworkjavascriptv1.0.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

add-module-exports
import addModuleExports from 'babel-plugin-add-module-exports' (in .babelrc or Babel config)
const addModuleExports = require('babel-plugin-add-module-exports') (only for programmatic use, not config)
Typically used as a string in Babel config: 'plugins': ['add-module-exports']
addDefaultProperty
In .babelrc: { 'plugins': [['add-module-exports', { 'addDefaultProperty': true }]] }
Passing options as a separate object without the plugin name array
Optional option to also set `module.exports.default` for backward compatibility with code using `.default`.

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.
Debug
Known issues
gotchaOnly 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'.
affects: >=1.0.0
gotchaDoes 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.
affects: >=1.0.0
deprecatedPlugin 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.
affects: >=1.0.0
gotchaIf 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.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'babel-plugin-add-module-exports'
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`.
Module build failed: Error: [BABEL] unknown plugin "add-module-exports"
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.
TypeError: Cannot read property 'default' of undefined
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.
Warning: add-module-exports plugin only works when exporting using `export default`.
The source file uses named exports or no default export.
fix
Ensure the file has a single `export default` statement.
Upgrade
Version history
1.0.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
12
OpenAI (training)
2
Resources
babel-plugin-add-module-exports — npm install babel-plugin-add-module-exports · libregistry