0

ES modules have the concept of "default export" and "default import", which looks like this:

export default foo;             // roughly sugar for: const tmp = foo; export { tmp as default }
import d from './myModule.js';  // roughly sugar for: import { default as d } from './myModule.js'

This concept is not one that I have met in any other module systems in other languages. It caused much confusion for me when I first met it, and it seems like it causes much confusion for other people too. The concept also seems to provide no additional power, compared to named exports.

So: why does this "default export" feature exist? What problem is it solving? The spec doesn't seem to provide any reasoning for it.

jameshfisher
  • 34,029
  • 31
  • 121
  • 167
  • 1
    I'm inclined to close as a duplicate of [Why does JavaScript have default exports?](https://stackoverflow.com/questions/57020740/why-does-javascript-have-default-exports), but you recently edited an post in there so you must remember it. How does my answer there not provide the reasoning you're looking for? – Bergi Oct 14 '20 at 15:09
  • 1
    Maybe (1) encourage a script-writer to write modules that do (and export) one thing, and one thing only (2) allow scripts to specify a *main thing* that they're exporting (eg React) but also have related side things they're exporting (eg useEffect). Whether those reasons are good enough to justify it is opinion-based, though – CertainPerformance Oct 14 '20 at 15:11
  • 1
    I think any answer would be speculation unless they were involved in writing the spec. I don't know that this is directly answerable. – zero298 Oct 14 '20 at 15:17
  • @CertainPerformance I would say it's meant to *enable* them to do that, not to *encourage* them. Modules that export only one thing were widely popular before ES6 was designed, and it was considered a problem to force the naming of exports in that common use case. – Bergi Oct 14 '20 at 15:20
  • 1
    @Bergi aha, I had a vague memory of seeing this question somewhere, but was unable to find it again! ‍♂️ – jameshfisher Oct 14 '20 at 15:34

0 Answers0