Using
index.{jsx,tsx}
for Components orindex.{js,ts}
for other files
- Hard to search for a component or file based on it’s name (the
index
file is only on the 7th result)
- Opening several
index
files in the IDE results in hard to read tabs with needlessindex
keywords
- ES Modules in browsers (based on the spec) do not support index files resolutions (have to pass the explicit file name and the extension)
- ES Modules in Node.js are compliant to the spec, so also doesn’t support it
- Ryan Dahl, the creator of Node.js, talks about things he regrets in the Node.js design and one of them is adding the
index
resolution feature
- Requires more discipline or linting to enforce proper convention
- It’s easy to fall for an indirection pattern where you import some stuff from other files and export them in different names
- Which makes it harder to navigate the code with Jump to Definition or Find References
- Confusing to choose between importing from the index or explicitly from the target file
- Sometimes we don’t need everything from the index but only 1 thing
- Importing needless stuff can bloat up the bundle size
- Other good reasons against it:
TypeScript does support it out of the box, but we have to set moduleResolution: "node"
if we set module
to es2015
or above