Folder structures that explain the codebase
How folder layout helps or hurts understanding: grouping by feature or by type, naming entry points, keeping shared code honest and making the architecture visible.
A folder structure is the first thing anyone sees in a repository, and it's documentation that can't go out of date. A good one tells newcomers what the system does and where to find things. A poor one hides both.
Group by feature, not only by type
Two common approaches:
By type:
src/
components/
hooks/
services/
utils/By feature:
src/
checkout/
catalog/
accounts/
shared/Grouping by type works for small projects. As a codebase grows, it scatters each feature across many folders, so understanding checkout means opening five directories. Grouping by feature keeps a feature's components, logic, API calls and tests together, and the top level tells you what the product does.
Many teams combine them: features at the top, types inside each feature.
Make entry points obvious
Readers look for where things start. Help them:
- Name entry files plainly:
server.ts,worker.ts,cli.ts. - Keep framework conventions (
app/,pages/,routes/) intact rather than hiding them behind custom setups. - Put scripts in a
scripts/folder with a one-line comment at the top of each.
Keep "shared" small and honest
utils, common, lib and shared folders tend to collect everything that didn't have an obvious home. Keep them for genuinely shared code, and move feature-specific helpers into their features. A 3,000-line utils.ts is a smell.
Mirror tests to source
Either colocate tests (pricing.ts next to pricing.test.ts) or mirror the source tree under test/. Both work; mixing them makes tests hard to find.
Show boundaries
If parts of the code must not depend on each other (the UI on the database, one feature on another's internals), make that visible: an index.ts per feature that exports its public API, and a lint rule (such as dependency-cruiser or Nx module boundaries) that enforces it.
Name for the domain
Folder names should use the words the business uses: invoices, not billingDocs2. A domain glossary in the README helps when names are unavoidable jargon.
Document the structure in the README
Five lines in the README, one per top-level folder, save every newcomer a tour:
src/checkout/ Cart, pricing and order placement
src/catalog/ Products, search and inventory
src/accounts/ Sign-in, profiles and settings
src/shared/ Design system, API client, config
src/jobs/ Background workers (run with npm run worker)Check what a newcomer sees
Ask RepoGrasp "give me an architecture overview of this repo" and see whether its explanation matches what you intend the structure to communicate. If the overview is confusing, the structure probably is too.
Our codebase guides show the conventional layout for more than 70 stacks.