What is the benefit of prefer-default-export?
The docs don't have a Why section for prefer-default-export, and I'm not seeing the benefit of it on my own. I would think that not using default is preferred. With default exports, you lose refactoring power (if you rename the source const/function/class, it won't rename default imports).
As more of an edge case: it makes code less future-proof. i.e. if you create a file that will be a collection of errors, but it only starts with one error, to follow the linting rules, you'll have to have it export default, but then when you add the 2nd error at a later time, you'll have to do a bunch of refactoring that could have been prevented if the recommendation was to avoid default export.
One of the strongest arguments against default exports, which was mentioned above but seems to have got a bit lost in the noise, is that renaming named exports is much more explicit than renaming a default. Which means you can definitively find all imports with a simple grep for ExportedName as. With default exports it's much harder to find all usages.
This alone is a big enough reason to ban default exports for me.
@gregplaysguitar i responded to that right below it: https://github.com/airbnb/javascript/issues/1365#issuecomment-351476589
With either case, you'd want to use a tool that understands export specifiers, because of export from declarations - so that's not a reason to ban default exports either. Regardless, renaming is fine.
If for whatever reason you don't want the consumer to change the "name" of your "API", then named exports are not much better than default exports. Export a class for that, so that the class methods become the "API" (again, doesn't matter if that export itself is default or named).
This has nothing to do with linters, or export from, or exporting classes.
Consider two scenarios
export default MyThing
and
export MyThing
In the former, to find all usages I need to grep for imports by import path which varies with relative imports, so isn't consistent. Then I have to scan the imports to see what the imported names are, then I have to look for the imported names to see where they're used.
In the latter it's as simple as grep -r MyThing ./, and I can see all the usages (and maybe explicit renames) in one list.
(This is also the very reason why it's much easier for "Find references" tools like flow's to find usages of named exports)
@gregplaysguitar "find references" tools build a dependency graph, and then use specifiers - they don't do grepping, so that's irrelevant. Can you elaborate on your use cases where you want to find all usages, and why, and how using eslint-plugin-import as defined in the airbnb config won't report on those if they're no longer valid?
I'm working with a large legacy codebase where we frequently refactor old code, and need to understand where and how it's used in order to facilitate that. The prevailing pattern is default exports and for a few reasons our IDE setup doesn't just find all of these for for us; in any case grep is simpler and my position is that default exports make grokking the code much more difficult due to what I outlined above. So going forward we're preferring non-default exports.
All of this isn't meant to be an argument that you or others should change your practice, it just seems to me like "grepping for references" is a valid advantage of named exports which hasn't really been acknowledged. Dependency graphs are great, but I don't want to depend on a complex tool if a simple one can mostly do the job.
How is grepping the last part of the import specifier (e.g. someModule.js in /path/to/someModule.js) any worse than grepping the imported name? It seems like grepping for someModule.js is guaranteed to find all files that actually import someModule.js.
(Sure you might have multiple files called someModule.js but then you'd have multiple import { someModule } as well so I feel like that's moot).
That's a fair point, and perhaps file-naming consistency is part of our issue here too, but you're still left with the additional step of searching for usage after you've found the imports. Really I think it boils down to the merits of renaming, which default exports encourages. I'd prefer a given thing to keep the same name throughout the codebase. But each to their own.
@gregplaysguitar to be honest, i agree with you - but the way to enforce that is with a linter rule, one that can catch both default import names and named import renames, not by avoiding one of the critical pieces of ES Modules: the default export, which is what the module is (named exports being what the module has).
Since avoiding defaults can't guarantee "the same name throughout the codebase", while a linter rule can whether you use defaults or not, then avoiding defaults for that reason makes no sense.
Since avoiding defaults can't guarantee "the same name throughout the codebase", while a linter rule can whether you use defaults or not, then avoiding defaults for that reason makes no sense.
But if the goal is name parity, named exports does make more sense... if you misspell, or miss one in a refactor, the code won't work and you'd catch it quickly. Anything above a very basic text editor will catch an error like that out of the box.
To enforce a name change with defaults, it is more work. Gotta set up a linter, make sure people lint, or maybe CI does, etc. One is a matter of semantics, the other is not.
I'm the one that opened this issue, but my concern was just wanting it to be documented. I don't agree with it, but at least you all have your reasoning, and it's now documented (tho again, I think the argument in the official documentation is weak!). We all of course know that we can just change the setting, but I do think we (as JS devs) should advocate for named exports, however, it's clearly very subjective.
But I do enjoy this thread getting resurrected from time to time. 😄
For those who prefer switching it off, you can edit your .eslintrc.json
{
...
"extends": "airbnb-base",
"rules": {
"prefer-default-export": ["warn"],
},
...
}
Quien eres
noreply
Talking about refactoring (which has been often mentioned as reason for avoiding default exports) I don't think it makes things more difficult if you use default exports.
You can still look for the path if you wanna see where an export has been used. If a module has just one reasonable export (e.g. a class) I wouldn't avoid a default export just because it's 'bad'.
From @basarat: export default can lead to problems
@dandv that article has a number of points that don't hold up to scrutiny:
If you refactor Foo in foo.ts it will not rename it in bar.ts.
this is true of named exports as well, hence, irrelevant.
If you end up needing to export more stuff from foo.ts (which is what many of your files will have) then you have to juggle the import syntax.
this is also true of named exports - adding , { bar } to import foo is no harder than adding , bar to import { foo }.
Discoverability is very poor for default exports. You cannot explore a module with intellisense to see if it has a default export or not. You even get autocomplete
This means intellisense is broken, not default exports. A competent IDE understands what is at this point a 3+ year old standard.
Better commonJS experience.
This is just nonsense; a CJS module only has a default export conceptually, despite babel's interop, and the discussion is about authoring a module, not consuming one.
You don't get typos
That's not a typo, that's just people naming variables differently, which is a) fine, and b) can be enforced by a linter if that's something you want to do.
Auto import quickfix works better
Tools like importjs work identically with named and default imports, so this is just false on its face.
Re-exporting is unnecessarily hard
I wouldn't recommend re-exporting in the first place, but export { default } from 'foo' as an extra step really doesn't seem that hard to me - and I'd actually say it's better to be explicit than to use export *.
@ljharb: thank you for addressing those. How about Dynamic imports() forcing access to properties via .default?
@dandv you have to specify a name with everything in import() - but if you author the module, you can easily do this:
export default Foo;
export function then(resolve) { return resolve(Foo); }
and then you get the default export directly from import(). Next?
this is true of named exports as well, hence, irrelevant.
Not true.
Default
export
export default const foo = 123;
use
import foo from './foo';
Rename foo => bar in export will not rename use.
Named
export
export const foo = 123;
use
import { foo } from './foo';
Rename foo => bar in export will rename use :rose:
@ljharb that example is from the document originally shared. Restored comment at your request ^ :rose:
this is also true of named exports - adding , { bar } to import foo is no harder than adding , bar to import { foo }
Not harder in terms of character count. But harder in terms of concept overhead. Why is one a default and the other named when both could be named. What about when you have three items, which one gets to be the default? Not worth the extra concept overhead. :rose: