Skip to content

Commit 9fa0ef1

Browse files
authored
Update README.md
1 parent 1035d31 commit 9fa0ef1

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@
44

55
Testing best way to support cjs and esm and how to structure project to enable that (see results.txt for more info)
66

7+
8+
## Avoid Default Exports and Prefer Named Exports'
9+
10+
### Context
11+
12+
When CommonJS was the primary authoring format, the best practice was to export only one thing from a module using the module.exports = ... format. This aligned with the UNIX philosophy of "Do one thing well". The module would be consumed (const localName = require('the-module');) without having to know the internal structure.
13+
14+
Now, ESModules are the primary authoring format. They have numerous benefits, such as compile-time verification of exports, and standards-defined semantics. They have a similar mechanism known as "default exports", which allows for a consumer to import localName from 'the-module';. This is implicitly the same as `import { default as localName } from 'the-module';`.
15+
16+
However, there are numerous reasons to avoid default exports, as documented by others before:
17+
18+
> NOTE. https://humanwhocodes.com/blog/2019/01/stop-using-default-exports-javascript-module/
19+
20+
### Summary
21+
22+
They add indirection by encouraging a developer to create local names for modules, increasing cognitive load and slowing down code comprehension: import TheListThing from 'not-a-list-thing';.
23+
- They thwart tools, such as IDEs, that can automatically rename and refactor code.
24+
- They promote typos and mistakes, as the imported member is completely up to the consuming developer to define.
25+
- They are ugly in CommonJS interop, as the default property must be manually specified by the consumer. This is often hidden by Babel's module interop.
26+
- They break re-exports due to name conflicts, forcing the developer to manually name each.
27+
- Using named exports helps prevent needing to rename symbols, which has myriad benefits. A few are:
28+
29+
IDE tools like "Find All References" and "Go To Definition" function
30+
Manual codebase searching ("grep", etc) is easier with a unique symbol
31+
732
## Decision
833

934
> source: https://backstage.io/docs/architecture-decisions/adrs-adr004

0 commit comments

Comments
 (0)