name: design-frontend-project-structure
description: Use when starting a frontend app (React, Next.js, Vue/Nuxt, Angular, Svelte) or when an existing one has become hard to navigate — huge flat components/ and hooks/ folders, features scattered across type-based directories, unclear where API calls or state live, or barrel files slowing builds.
source: 'Dodds, "Colocation", kentcdodds.com, 2019; React Documentation (legacy), "File Structure"; Next.js Documentation, "Project Structure and Organization"; Angular, "Style Guide"; alan2207, "Bulletproof React" (GitHub); Vite Documentation, "Performance — Avoid Barrel Files"; Feature-Sliced Design, feature-sliced.design'
tags: [project-structure, frontend, react, colocation, feature-folders, maintainability]
related: [apply-package-by-feature, apply-feature-sliced-design, design-component, design-component-api, apply-module-boundary-enforcement, design-monorepo-structure]
Design Frontend Project Structure
Structure a frontend in three tiers: app (routing, providers, global setup), features (self-contained business areas that colocate their components, hooks, API calls, state, and tests), and shared (generic UI kit, utilities, API client). Imports flow one way: app → features → shared.
Why This Is Best Practice
Adopted by:
- Bulletproof React (one of the most-starred React architecture references).
- Angular's official style guide (folders-by-feature, LIFT).
- Next.js App Router (route-based colocation, private
_folders, route groups).
- Feature-Sliced Design (a formalized version of the same layering).
Impact:
- Colocation (keeping code near where it's used) reduces the cost of finding, changing, and deleting code (Dodds, 2019).
- Avoiding barrel files is recommended by Vite's performance guide, because re-export files force the bundler and dev server to load far more modules than needed.
Why best:
- Grouping by file type (
components/, hooks/, services/) scales poorly. Every feature is spread across every folder, and nothing signals what's safe to delete.
- Feature folders with a one-way dependency rule keep features independent, make ownership clear, and let you remove a feature by deleting a folder.
Sources: Dodds, K.C. (2019), "Colocation". React docs, "File Structure" (legacy.reactjs.org). Next.js docs, "Project Structure and Organization". Angular Style Guide. Bulletproof React (github.com/alan2207/bulletproof-react). Vite docs, "Performance".
Steps
Start simple and grow. Under about 15 components, a flat src/components/ plus src/pages/ is fine (React's own docs warn against overthinking it). Introduce the three-tier structure as soon as distinct business areas appear.
Use the three-tier layout:
src/
app/ # entry, router/routes, providers, global styles, error boundary
routes/ (or Next.js app/)
providers.tsx
features/
cart/
components/ # CartDrawer.tsx, CartItem.tsx
hooks/ # useCart.ts
api/ # getCart.ts, addToCart.ts (data fetching + query keys)
store/ # feature state slice (if needed)
types.ts
utils.ts
cart.test.tsx
index.ts # public API (optional; see step 6)
checkout/
search/
shared/ (or components/ + lib/)
ui/ # Button, Modal, Input: design-system primitives, no business logic
lib/ # api client, date/format utils, analytics wrapper
hooks/ # generic hooks (useDebounce, useMediaQuery)
config/ # env access, constants
assets/
test/ # test utils, MSW handlers, setup
Enforce one-way imports:
app may import features and shared.
features may import shared, and other features only through their public index.ts (better: compose them in app or routes).
shared imports nothing above it.
Add an ESLint rule (import/no-restricted-paths, eslint-plugin-boundaries) to enforce this (see apply-module-boundary-enforcement).
Keep routes/pages thin. A page composes feature components and handles layout and route params. Business logic and data fetching live in the feature.
Separate server state from client state.
- Server data goes through a data-fetching layer (TanStack Query, SWR, RTK Query, Apollo, framework loaders), defined in each feature's
api/.
- Global client state (Zustand, Redux, Pinia) only for true cross-feature UI state.
- Local state stays in components.
Use barrel files sparingly. A small index.ts per feature as its public API is fine. Avoid big re-export barrels in shared/ or components/, which slow dev servers and tests and break tree-shaking (Vite guide). Prefer direct imports with path aliases (@/shared/ui/Button).
Colocate tests, stories, and styles next to components (Button.tsx, Button.test.tsx, Button.stories.tsx, Button.module.css).
Set naming conventions. PascalCase for component files, camelCase for hooks (useX) and utils, kebab-case or camelCase folders consistently. Use one component per file for exported components.
Follow framework routing conventions:
- Next.js App Router:
app/ for routes only; use _components private folders and (group) route groups. Keep features in src/features.
- Nuxt:
pages/, components/ auto-imports, composables/. Group by feature subfolders.
- Angular: standalone components with feature folders and lazy-loaded routes.
For larger apps, adopt a formal methodology such as Feature-Sliced Design (apply-feature-sliced-design), or split into monorepo packages (design-monorepo-structure).
Rules
- Group by feature first, file type second (inside the feature).
- Dependencies flow app → features → shared, never upward.
shared/ui holds no business logic or feature-specific API calls.
- Server state is managed by a data-fetching layer, not copied into global stores.
- Deleting a feature should mostly mean deleting its folder.
Common Mistakes
- One giant
components/ folder with hundreds of files mixing primitives and feature UI.
- Feature-to-feature deep imports (
features/cart/components/internal/... used by checkout). Hidden coupling.
- Global store for everything. Server data duplicated in Redux and going stale. Use a server-state library.
- Barrel-everything.
export * chains cause slow HMR, circular imports, and bloated test runs.
- API calls inside presentational components. Hard to test and reuse. Keep them in feature
api/ hooks.
Examples
React + Vite + TanStack Query: features/products/api/useProducts.ts defines the query, features/products/components/ProductGrid.tsx renders it, app/routes/products.tsx composes ProductGrid and FiltersPanel, and shared/ui/Card.tsx is a design-system primitive.
Next.js App Router: app/(shop)/products/page.tsx imports from src/features/products. Server actions live in features/products/actions.ts. app/(shop)/products/_components/ holds route-only pieces.
When NOT to Use
- Tiny apps and prototypes. A flat structure is faster to work in.
- Micro-frontend architectures, where each micro-app follows this internally but the top-level split is per team or app (see
design-monorepo-structure).
1---2name: design-frontend-project-structure3description: Use when starting a frontend app (React, Next.js, Vue/Nuxt, Angular, Svelte) or when an existing one has become hard to navigate — huge flat components/ and hoo4---5---6name: design-frontend-project-structure7description: Use when starting a frontend app (React, Next.js, Vue/Nuxt, Angular, Svelte) or when an existing one has become hard to navigate — huge flat components/ and hooks/ folders, features scattered across type-based directories, unclear where API calls or state live, or barrel files slowing builds.8source: 'Dodds, "Colocation", kentcdodds.com, 2019; React Documentation (legacy), "File Structure"; Next.js Documentation, "Project Structure and Organization"; Angular, "Style Guide"; alan2207, "Bulletproof React" (GitHub); Vite Documentation, "Performance — Avoid Barrel Files"; Feature-Sliced Design, feature-sliced.design'9tags: [project-structure, frontend, react, colocation, feature-folders, maintainability]10related: [apply-package-by-feature, apply-feature-sliced-design, design-component, design-component-api, apply-module-boundary-enforcement, design-monorepo-structure]11---1213# Design Frontend Project Structure1415Structure a frontend in three tiers: **app** (routing, providers, global setup), **features** (self-contained business areas that colocate their components, hooks, API calls, state, and tests), and **shared** (generic UI kit, utilities, API client). Imports flow one way: app → features → shared.1617## Why This Is Best Practice1819**Adopted by:**20- Bulletproof React (one of the most-starred React architecture references).21- Angular's official style guide (folders-by-feature, LIFT).22- Next.js App Router (route-based colocation, private `_folders`, route groups).23- Feature-Sliced Design (a formalized version of the same layering).2425**Impact:**26- Colocation (keeping code near where it's used) reduces the cost of finding, changing, and deleting code (Dodds, 2019).27- Avoiding barrel files is recommended by Vite's performance guide, because re-export files force the bundler and dev server to load far more modules than needed.2829**Why best:**30- Grouping by file type (`components/`, `hooks/`, `services/`) scales poorly. Every feature is spread across every folder, and nothing signals what's safe to delete.31- Feature folders with a one-way dependency rule keep features independent, make ownership clear, and let you remove a feature by deleting a folder.3233Sources: Dodds, K.C. (2019), "Colocation". React docs, "File Structure" (legacy.reactjs.org). Next.js docs, "Project Structure and Organization". Angular Style Guide. Bulletproof React (github.com/alan2207/bulletproof-react). Vite docs, "Performance".3435## Steps36371. **Start simple and grow.** Under about 15 components, a flat `src/components/` plus `src/pages/` is fine (React's own docs warn against overthinking it). Introduce the three-tier structure as soon as distinct business areas appear.38392. **Use the three-tier layout:**40 ```41 src/42 app/ # entry, router/routes, providers, global styles, error boundary43 routes/ (or Next.js app/)44 providers.tsx45 features/46 cart/47 components/ # CartDrawer.tsx, CartItem.tsx48 hooks/ # useCart.ts49 api/ # getCart.ts, addToCart.ts (data fetching + query keys)50 store/ # feature state slice (if needed)51 types.ts52 utils.ts53 cart.test.tsx54 index.ts # public API (optional; see step 6)55 checkout/56 search/57 shared/ (or components/ + lib/)58 ui/ # Button, Modal, Input: design-system primitives, no business logic59 lib/ # api client, date/format utils, analytics wrapper60 hooks/ # generic hooks (useDebounce, useMediaQuery)61 config/ # env access, constants62 assets/63 test/ # test utils, MSW handlers, setup64 ```65663. **Enforce one-way imports:**67 - `app` may import `features` and `shared`.68 - `features` may import `shared`, and other features only through their public `index.ts` (better: compose them in `app` or routes).69 - `shared` imports nothing above it.7071 Add an ESLint rule (`import/no-restricted-paths`, eslint-plugin-boundaries) to enforce this (see `apply-module-boundary-enforcement`).72734. **Keep routes/pages thin.** A page composes feature components and handles layout and route params. Business logic and data fetching live in the feature.74755. **Separate server state from client state.**76 - Server data goes through a data-fetching layer (TanStack Query, SWR, RTK Query, Apollo, framework loaders), defined in each feature's `api/`.77 - Global client state (Zustand, Redux, Pinia) only for true cross-feature UI state.78 - Local state stays in components.79806. **Use barrel files sparingly.** A small `index.ts` per feature as its public API is fine. Avoid big re-export barrels in `shared/` or `components/`, which slow dev servers and tests and break tree-shaking (Vite guide). Prefer direct imports with path aliases (`@/shared/ui/Button`).81827. **Colocate tests, stories, and styles** next to components (`Button.tsx`, `Button.test.tsx`, `Button.stories.tsx`, `Button.module.css`).83848. **Set naming conventions.** PascalCase for component files, camelCase for hooks (`useX`) and utils, kebab-case or camelCase folders consistently. Use one component per file for exported components.85869. **Follow framework routing conventions:**87 - **Next.js App Router:** `app/` for routes only; use `_components` private folders and `(group)` route groups. Keep features in `src/features`.88 - **Nuxt:** `pages/`, `components/` auto-imports, `composables/`. Group by feature subfolders.89 - **Angular:** standalone components with feature folders and lazy-loaded routes.909110. **For larger apps, adopt a formal methodology** such as Feature-Sliced Design (`apply-feature-sliced-design`), or split into monorepo packages (`design-monorepo-structure`).9293## Rules9495- Group by feature first, file type second (inside the feature).96- Dependencies flow app → features → shared, never upward.97- `shared/ui` holds no business logic or feature-specific API calls.98- Server state is managed by a data-fetching layer, not copied into global stores.99- Deleting a feature should mostly mean deleting its folder.100101## Common Mistakes102103- **One giant `components/` folder** with hundreds of files mixing primitives and feature UI.104- **Feature-to-feature deep imports** (`features/cart/components/internal/...` used by checkout). Hidden coupling.105- **Global store for everything.** Server data duplicated in Redux and going stale. Use a server-state library.106- **Barrel-everything.** `export *` chains cause slow HMR, circular imports, and bloated test runs.107- **API calls inside presentational components.** Hard to test and reuse. Keep them in feature `api/` hooks.108109## Examples110111**React + Vite + TanStack Query:** `features/products/api/useProducts.ts` defines the query, `features/products/components/ProductGrid.tsx` renders it, `app/routes/products.tsx` composes `ProductGrid` and `FiltersPanel`, and `shared/ui/Card.tsx` is a design-system primitive.112113**Next.js App Router:** `app/(shop)/products/page.tsx` imports from `src/features/products`. Server actions live in `features/products/actions.ts`. `app/(shop)/products/_components/` holds route-only pieces.114115## When NOT to Use116117- **Tiny apps and prototypes.** A flat structure is faster to work in.118- **Micro-frontend architectures**, where each micro-app follows this internally but the top-level split is per team or app (see `design-monorepo-structure`).