# babel-plugin-add-module-exports

> Fix babel/babel#2212

Latest version **1.0.4** (published 2020-09-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install babel-plugin-add-module-exports
pnpm add babel-plugin-add-module-exports
yarn add babel-plugin-add-module-exports
bun add babel-plugin-add-module-exports
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2020-09-08 |
| First published | 2015-11-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 21.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 720 |
| Author | 59naga |
| Maintainers | 59naga, lijunle, ljharb |
| Keywords | babel-plugin, module.exports |

## Links

- npm: https://www.npmjs.com/package/babel-plugin-add-module-exports
- Repository: https://github.com/59naga/babel-plugin-add-module-exports
- Homepage: https://github.com/59naga/babel-plugin-add-module-exports#readme
- Issues: https://github.com/59naga/babel-plugin-add-module-exports/issues
- npm.io page: https://npm.io/package/babel-plugin-add-module-exports

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2020-09-08
- 0.2.1 (legacy) — 2016-05-12
- 1.0.3 — 2020-09-08
- 1.0.2 — 2019-04-17
- 1.0.0 — 2018-09-11
- 0.3.3 — 2018-08-14
- 0.3.2 — 2018-07-16
- 0.3.1 — 2018-07-02
- 0.3.0 — 2018-07-02
- 0.3.0-pre.2 — 2018-07-01
- 0.3.0-pre.1 — 2018-06-25
- 0.3.0-pre — 2018-06-25
- 0.2.0 — 2016-05-06
- 0.1.4 — 2016-04-26
- 0.1.3 — 2016-04-25
- … 8 more at https://npm.io/package/babel-plugin-add-module-exports/versions

## README

babel-plugin-add-module-exports
---

<p align="right">
  <a href="https://npmjs.org/package/babel-plugin-add-module-exports">
    <img src="https://img.shields.io/npm/v/babel-plugin-add-module-exports.svg?style=flat-square">
  </a>
  <a href="https://travis-ci.org/59naga/babel-plugin-add-module-exports">
    <img src="http://img.shields.io/travis/59naga/babel-plugin-add-module-exports.svg?style=flat-square">
  </a>
</p>

Why?
---

Babel@6 doesn't export default `module.exports` any more - [T2212 *Kill CommonJS default export behavior*](https://phabricator.babeljs.io/T2212).

Babel@6 transforms the following file

```js
export default 'foo'
```

into

```js
'use strict';
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 'foo';
```

Therefore, it is a need to use the ugly `.default` in node.js.

```js
require('./bundle.js') // { default: 'foo' }
require('./bundle.js').default // 'foo'
```

This plugin follows the babel@5 behavior - add the `module.exports` if **only** the `export default` declaration exists.

```js
'use strict';
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 'foo';
module.exports = exports['default'];
```

Therefore, our old codes still work fine - the `.default` goes away. :wink:

```js
require('./bundle.js') // foo
```

Usage
---

Install this plugin from npm:

```bash
npm install babel-plugin-add-module-exports --save-dev
# or
yarn add -D babel-plugin-add-module-exports
```

Write the name to [babelrc](https://babeljs.io/docs/usage/babelrc/). It works with [preset-env](http://babeljs.io/docs/en/babel-preset-env/) to output CommonJS code:

```json
{
  "presets": ["@babel/env"],
  "plugins": ["add-module-exports"]
}
```

### modules: false

**However, the plugin doesn't change the pure-esmodule**.
this plugin makes changes only when exists `exports.default` (in other words, using [commonjs](https://babeljs.io/docs/en/babel-plugin-transform-es2015-modules-commonjs/)).

```json
{
  "presets": [["@babel/env", { "modules": false }]],
  "plugins": ["add-module-exports"]
}
```

into

```js
export default 'foo'
```

`1.0.0` Currently support is `commonjs` and `umd`.
Doesn't support `amd`, `systemjs` modules(don't use. there are no plans to support at the moment).

### with Webpack

Likewise, webpack doesn't perform commonjs transformation for [codesplitting](https://webpack.js.org/guides/code-splitting/). Need to set commonjs conversion.

```json
{
  "presets": [["@babel/env", { "modules": "commonjs" }]],
  "plugins": ["add-module-exports"]
}
```

Options
---

## `addDefaultProperty`

If you're exporting an object and wish to maintain compatibility with code using the `require('./bundle.js').default` syntax, you can optionally enable the `addDefaultProperty` option as follows:

```json
{
  "presets": ["env"],
  "plugins": [
    [
      "add-module-exports",
      {
        "addDefaultProperty": true
      }
    ]
  ]
}
```

This will cause a second line of code to be added which aliases the `default` name to the exported object like so:

```js
module.exports = exports['default'];
module.exports.default = exports['default']
```

See also
---
* [babel-plugin-experimental-syntax-dynamic-import](https://github.com/59naga/babel-plugin-experimental-syntax-dynamic-import)

License
---
[MIT](http://59naga.mit-license.org/)

---
_Source: https://npm.io/package/babel-plugin-add-module-exports · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
