ES module is a .js
file with at least one export or folder containing index.js
with at least one export.
The guiding module structure design principle is: you should imagine that any module might be extracted to its own package some day.
In index.js
we define a module interface, its exported object. This file should not contain business logic.
Placing business logic in index.js
leads to following inconveniences, making code reading harder:
- If
components/Compo/index.js
is opened in a tab in IDE, in many of them name of the tab would be "index.js", which is non-descriptive. There are addons in most IDEs that allow to show "Compo/index.js" instead of just "index.js" in tab name, but "Compo.js" is still more succinct yet descriptive.