16

What is the best way to re-export a default export from another file?

I'd like to do something like this:

export Button from './Button/Button';

Apparently this is not valid EcmaScript syntax (however it used to work in older Babel versions).

As a result I want to import {Button} from 'components';

Is there a better way, maybe a one-liner? ES6 syntax would be preferred.

Thanks!

amann
  • 5,449
  • 4
  • 38
  • 46
  • 1
    Why? Why not just `import {Button} from 'components/Button'` or something? – Madara's Ghost Jan 03 '16 at 11:59
  • Does this answer your question? [Re-export default in ES 6 modules](https://stackoverflow.com/questions/39999282/re-export-default-in-es-6-modules) – A1rPun Oct 16 '22 at 23:29

1 Answers1

21

You can use the export { x as y } syntax:

export { default as Button } from './Button/Button';

I'd like to do something like this:

export Button from './Button/Button';

There is a stage 1 proposal for this syntax: https://github.com/leebyron/ecmascript-more-export-from

Nicolò
  • 1,795
  • 1
  • 16
  • 16
  • Unfortunately `export { default as y } from './m'` does not work for me. I have `export default function () { /* ... */ }` in *m.js*. With the `export { default as ...` statement in *i.js*, I try `import { y } from './i'` in another file, but y is undefined. I use babel 6 require-hook and polyfill in node 4.2. – x-ray Jan 09 '16 at 03:50
  • Nevermind, found the reason: The [add-module-exports plugin](https://github.com/59naga/babel-plugin-add-module-exports) is the reason for `export { default as x } from '...'` not to work. – x-ray Jan 09 '16 at 04:05