4

I want to export some module like below way but always failed..

foo.js

const foo = {
  a: 'b'
};
export default foo;

index.js

export foo from './foo';  // encounter error here
export * from './foo';    // it works..

I don't know why can't I use the first method to export module from foo.js, in my opinion, I can export anything like func, class, variables etc..

sdgluck
  • 24,894
  • 8
  • 75
  • 90
L.Jovi
  • 1,631
  • 4
  • 22
  • 36

1 Answers1

14

To export a default export of one module as a named export of another you must do:

// index.js
export { default as foo } from './foo';

You can now import foo as a named export elsewhere:

// another.js
import { foo } from './index'
sdgluck
  • 24,894
  • 8
  • 75
  • 90
  • thanks for your reply. – L.Jovi Dec 14 '16 at 10:51
  • Although I understand the example, I'd like to know if there's any reason for the curly braces here. Why is not possible the following (without using Babel)? `export default from './foo';` (in _index.js_) and then import it as `import myDefault from './index.js' ` (in _another.js_) – Barleby Mar 15 '19 at 12:05