01 / 13
A feature-based structure, file naming and import rules.
A folder structure has a simple job: when you are about to change something, finding the relevant files in one place. If you group files not by their type (components/, hooks/, utils/) but by the feature they belong to (the cart, the product list, checkout), changing a feature is done in a single folder. The principle is the one in Kent C. Dodds's colocation article: put code as close as possible to where it is relevant.1
This guide covers the feature-based structure, the naming and the import rules that work the same way in Next.js and TanStack Start.
| By type | By feature | |
|---|---|---|
| The structure | Every feature's files under components/, hooks/, stores/ | All the cart's files under features/cart/ |
| While changing the cart | You hunt for files in several large folders | It is done in one folder |
| As it grows | One feature spreads across many folders | The feature stays in one place |
Arranging by type is common because it is easy to start with in frameworks such as Next.js and Nuxt. But as the project grows, changing one feature means touching many large folders.2
Independent sources recommend arranging by feature. bulletproof-react recommends collecting most of the code under features/, to stop feature code mixing with the shared components.3 Feature-Sliced Design turns the same idea into a stricter methodology: the code is split into layers, and the layers into highly cohesive slices.4
It is talked about on conference stages too. At Conf42, Aleksandr Guzenko explains with this methodology how architectural debt can be stopped from piling up by limiting the dependencies.5 At HolyJS, Alexander Morgunov covers the method's concepts (modularity, the public API, separating the infrastructure from the business logic) together with its weak spots (cross-imports between modules, insufficient documentation, code scattered across layers).6 So the approach is not an indisputable recipe but a choice with trade-offs.
ViraStack uses this structure too. A simplified version of its template:7
src/
├── app/ | routes/ # the routing
├── features/ # the feature modules
├── components/ # shared components (ui/, layout/, shared/)
├── hooks/ # shared hooks
├── lib/ # configured library instances (the API client, date-fns)
├── helpers/ # pure functions with no package imports
├── schemas/
├── stores/
├── types/
├── constants/
├── config/
├── providers/
├── styles/
└── env.tsBefore opening a new folder, look at whether it fits one of the existing ones.7
The real decision is that the project code sits outside the routing folder:
| Next.js | TanStack Start | |
|---|---|---|
| The routing folder | app/ | src/routes/ |
| The options the framework offers | The project code can be outside app, inside app, or split by route8 | Files and folders prefixed with - do not enter the route tree, and are used to hold code in route folders9 |
| The framework's own special names | page, layout, route, _folder, (group), [param]8 | __root, $param, the _ prefix (a pathless layout), the - prefix9 |
Next.js has no opinion on the structure; it asks you to stay consistent with the strategy you pick.8 In this structure, folders such as features/ and components/ sit at the same level as app/ and routes/, which is the "keep the project code outside app" strategy.7 The src/ folder is optional in Next.js and separates the application code from the configuration files in the root directory.8
features/cart/
├── api/ # the request functions and API hooks
├── components/
├── hooks/
├── schemas/ # the Zod schemas
├── stores/
├── types/
└── index.tsAlongside these there can be constants/, data/ and helpers/ too.7 Not every feature needs all of them; only open the folder you need.3
The test file sits next to the file it tests (cart-summary.test.tsx). The end-to-end tests, on the other hand, stay in the project root, because they are not tied to the src/ structure.1
Tip
A folder name can be a type; let the file name say the domain. Feature-Sliced Design counts generic folder names such as types/, components/ and utils/, and generic file names such as types.ts, utils.ts and helpers.ts, as a problem: code from different domains piles up in the same file.2 Write helpers/format-price.ts instead of helpers/utils.ts.
Everything starts inside features/[feature]/. Once a second feature needs the same code, the code moves to the global src/ layer (components/, lib/, hooks/).
Why does it move? Because features do not import from each other. Combining features is the page (application) layer's job, so every feature stays independent.3 Feature-Sliced Design also forbids slices in the same layer from using each other.4 The flow is one-way: shared code → features → the application.3
| Stays inside the feature | Moves to the global layer |
|---|---|
features/cart/components/cart-summary.tsx | components/ui/button.tsx |
| The function that calculates the cart total | The function that formats a currency (helpers/) |
| The cart API hooks | The shared API client (lib/api.ts) |
helpers/ is for pure functions with no package imports, and configured instances that use a package go under lib/.7
Tip
Why on the second use, not the third? The well-known "Rule of Three" works like this: the first time you write it, the second time you notice the repetition and copy it anyway, and the third time you extract it into shared code.10 In this structure the rule is stricter: if two features need the same code, the code moves to the global layer, because features cannot import each other.7
Warning
Not all code that looks alike is the same code. The cycle Sandi Metz describes: someone extracts the repetition into a shared function, then a parameter and a condition are added for the next new need, and the shared code becomes incomprehensible.11 For Metz, duplication is far cheaper than the wrong abstraction.12 Dan Abramov's The Wet Codebase talk tells the same trap as a story: the shared abstraction extracted to prevent copying accumulates special cases and becomes generic. His advice is not to be afraid of inlining a bad abstraction.13 Before you move it, ask: do the two uses share the same rule, or do they resemble each other by coincidence? Cleaning up coincidental duplication too early makes the code brittle.10
../// ❌ It changes with whichever folder you are in
import { Button } from '../../../components/ui/button'
// ✅ The same in every file
import { Button } from '@/components/ui/button'Next.js: create-next-app sets up the @/* alias by default.14
TanStack Start: an alias does not come by default. The example in the documentation uses ~/*. In Vite 8 and later there is built-in support that reads the tsconfig paths (off by default), and for Vite 7 and earlier the vite-tsconfig-paths plugin is needed.15
To enforce it: give ESLint's built-in no-restricted-imports rule { patterns: [{ regex: "^\\.\\./" }] }. Only the imports starting with ../ are forbidden, and sibling (./b) and subfolder (./lib/a) imports stay free.16 The import/no-relative-parent-imports rule is not suitable for this job: because it looks at the resolved file path, it counts imports written with @/ as errors too. In a trial the @/components/ui/button import failed for that reason.
Combine the features in the page:
// app/products/[id]/page.tsx
import { ProductDetail } from '@/features/products'
import { AddToCartButton } from '@/features/cart'Every feature exports the parts it opens to the outside through the index.ts at its root (see the index.ts section below).
There is no import from inside features/products to features/cart. You will see how to enforce this rule with ESLint in the Component and code organisation guide.
index.ts: a front door or a burden?In feature-based structures an index.ts is usually put at every feature's root. There are two views on this:
| A single entry (a public API) | A direct import | |
|---|---|---|
| Who argues for it | Feature-Sliced Design: the index file is the feature's outward contract17 | bulletproof-react: a direct import, because of tree shaking and performance problems in Vite3. Marvin Hagemeister: if barrels have spread everywhere, delete them all18. TkDodo: drop the barrel in application code19 |
| What you gain | When the feature's internal structure changes, the imports outside it do not break17 | Faster tools, clear dependencies20 |
| What it costs | The risk of circular imports, and slowdown in large projects1719 | Moving a file affects every import, and the encapsulation is weakened20 |
The measurements are clear in large projects. Marvin Hagemeister writes that in projects where barrels have spread to every folder, deleting them all can speed many tasks up by 60-80%. He adds that a few barrels are usually not a problem.18 TkDodo brought the number of modules one page loads in his own Next.js project down from about 11 thousand to 3.5 thousand (68%).19 Atlassian measured a 75% drop in build minutes per commit in the Jira frontend, and notes that in exchange they lost the packages' public API.20
Tip
The middle road:
A single, thin index.ts at the feature's root: named re-exports only, no export *.17
No separate barrel in the feature's subfolders (such as components/index.ts).17
The files of the same feature import each other directly by file path, not through index.ts. That is how circular imports are prevented.1719
Turn the import/no-cycle rule on.19
If the dev server or the tests get slow, the barrels are the first place to look.18
Files and folders in kebab-case: cart-summary.tsx, use-cart.ts. macOS and Windows do not distinguish upper and lower case; Linux (CI, the live server) does. An import that works locally can give a "Module not found" error in production.21 Pick one spelling and enforce it with lint (unicorn/filename-case).19
The component name in PascalCase, the file name in kebab-case: the tab-list.tsx file exports TabList.19
Do not touch the names the framework imposes: names such as page.tsx, layout.tsx, __root.tsx and $postId.tsx are part of the routing.89
Avoid generic names: instead of utils.ts, types.ts and helpers.ts, give a name that says the domain.2
The test file next to the source: cart-summary.test.tsx.1
Tip
Do not explain this structure to the agent every time; write it into a rules file. create-next-app adds an AGENTS.md file for coding agents itself.14 Lint rules such as the ../ ban also act as a check for the agent, which we covered in the Agentic coding guide.
Pick a feature from your project (the cart, say) and move its files under features/cart/. Only open the subfolders you need.
Find a function two features use (price formatting, say). Do they share the same rule, or do they resemble each other by coincidence? If it is the same, move it under helpers/.
Turn the ../ ban on with no-restricted-imports and change the imports containing ../ to the alias.
Count the index.ts files in your project. Turn the ones outside the feature roots into direct imports.
Watch Aleksandr Guzenko's Preventing architectural debt with Feature-Sliced Design talk and look at which of the dependency problems he describes exist in your project.5
Sources
The principle of putting code close to where it is relevant, the tests and the helper files. June 17, 2019.
The problems with arranging by type, and avoiding generic folder and file names.
The feature folder structure, the ban on imports between features, the one-way flow and the barrel note.
Layers, slices and the import rule between slices.
Tangled dependencies turning into architectural debt, and limiting that with Feature-Sliced Design. The slides are on the page.
The concepts, the public API and cross-imports, and the common problems such as weak documentation.
The directory structure and the rules in this guide, written as an agent rule.
The file conventions, the `src` folder, the private folders and the project organisation strategies.
The `__root`, `$`, `_` and `-` prefixes, and excluding a file from the route tree.
The "three strikes" rule, and the difference between necessary and coincidental duplication.
How the wrong abstraction comes about, and the way back. January 20, 2016.
The talk the claim that duplication is cheaper than the wrong abstraction comes from.
The cost of early abstraction, and inlining a bad abstraction. The conference page lists it under 2019; the event date on the page is April 2020.
The `@/*` alias, an absolute import example and `AGENTS.md`.
An alias not coming by default in Start, and the setup per Vite version.
Restricting import paths with `regex` inside `patterns`.
The index file as a public API, the `export *` problem and the costs of index files.
The effect of barrel files on tool performance. October 8, 2023.
Circular imports, the module count measurement and file name consistency. July 26, 2024.
The results measured in the Jira frontend, and the trade-offs. Tim Sebastian, June 26, 2025.
Imports that work locally and break in production because of case sensitivity.
Check the lesson's main ideas with three questions.
Your progress is stored only in this browser.
Last updated: November 3, 2026