Skip to content
Architecture & TypesChapter 20 / 57Intermediate5 min read

Project Structure

Project folder structure best practices for Next.js. A layout for src/app, src/lib modules, components, hooks, tests and env that stays predictable from day one.

On this page

1. What is this practice?

The goal of project structure is that any developer can guess where something lives, and where new code should go, without asking.

This is decided on day one. Once three modules are built in three different ways, fixing the structure later becomes a refactor of the whole project.

2. Why does it matter?

A real story: a new developer had to change a discount rule. The rule was in utils/helpers2.ts, a copy was in components/Cart/calc.ts, and a third was in api/checkout.ts. They changed one and two were left behind.

Another common problem: the repo still had the default create-next-app README, which listed wrong commands, and an env variable that the code reads was missing from .env.example. The app crashed on the production deploy.

No structurePredictable structure
Everything dumped into utils and helpers foldersEach module has its own named folder
PascalCase and kebab-case mixed in one folderOne naming convention per folder
Imports with a long line of dotsClean imports through an alias, like at-sign slash lib
A new developer keeps asking where to writeThey learn the pattern by looking at the code
A missing env variable is found in productionThe env module throws at boot

3. When to follow it

LevelHow importantWhy
Learning projectThe framework's default structure and a src/ folder are enoughIn a small project, extra folders are just noise
Production codeThe full layout on day one, with a README and .env.exampleThe team and the timeline both grow, and without structure the code scatters
Senior levelCapture the layout in a template or a generatorEvery new repo and module is born in the right shape

4. Bad vs good code

Bad: put wherever it fit

bash
app/page.tsx
app/api/checkout/route.ts
components/orderTable.tsx
components/order-form.tsx
components/Cart/calc.ts
utils/helpers.ts
utils/helpers2.ts
types.ts
api.ts

Naming is mixed, logic is scattered across components and utils, and types.ts is a shared dumping ground.

Good: each unit in its place

bash
README.md
.env.example
src/proxy.ts
src/app/layout.tsx
src/app/orders/page.tsx
src/app/api/orders/route.ts
src/app/api/orders/route.types.ts
src/lib/env.ts
src/lib/orders/orders.ts
src/lib/orders/orders.types.ts
src/lib/orders/orders.library.ts
src/lib/api/client/client.ts
src/components/OrderTable/OrderTable.tsx
src/components/OrderTable/OrderTable.types.ts
src/components/ui/button.tsx
src/hooks/use-orders/use-orders.ts
tests/api/orders.test.ts
tests/lib/orders.test.ts

Env is also read from a single module, which fails right away on a missing value:

ts
import "server-only";

export function requireEnv(name: string) {
  const value = process.env[name];

  if (!value) {
    throw new Error(`Missing environment variable ${name}`);
  }

  return value;
}

What is the difference?

Folder / fileJob
src/appOnly routes, layouts, metadata. Thin handlers, no logic
src/lib/<module>Business logic, types beside logic, pure helpers in .library.ts
src/components/<Name>One component, its props types, private children inside
src/components/uiDesign system primitives
src/hooks/use-<thing>Hooks for client state
src/proxy.tsReplaces middleware in Next.js 16, one light job only
.env.exampleThe name of every env variable, never the value
tests/api, tests/libEndpoint tests and pure unit tests kept apart

5. How to apply it

Day one of a new repo
Remove the template's old README and write one for your product
Set up the src folder and the at-sign slash alias
Create .env.example and an env module
Add the first module in src/lib with its types file
Delete the template's unused files

When adding a new module, just copy the shape of an existing one: folder, logic file, types file, test. When all modules look alike, the codebase feels small even when it is large.

Use one naming convention per folder. Components in PascalCase (OrderTable/OrderTable.tsx), hooks and lib modules in kebab-case (use-orders, render-quota). Design system primitives can have their own convention, but keep it the same across that whole folder.

6. Exceptions and common mistakes

Many teams prefer "feature folders": src/features/orders/ holding the component, hook and service together. This is common in mobile apps and works well. The layout above is organized by layer first (app, lib, components) and then by module. The trade-off: feature folders keep one feature in one place, while a layer layout keeps the line between server and client code clear. Both are fine, but use only one in a repo.

proxy.ts should do just one job, like refreshing a session or a redirect. The real security check should be done by every page and route handler itself.

Mistake: A folder named utils or helpers where everything goes

Why? The name does not say what is inside, and everything even slightly related ends up there.

Better: Keep the function near the module it serves, or create a new named module.

Mistake: process.env.KEY! scattered through the code

Why? The exclamation mark only silences the compiler. A missing value crashes somewhere far away at runtime.

Better: Use one env module that validates at boot and gives a clear error when a value is missing.

Mistake: Leaving the template files and README in place

Why? New developers get wrong commands and unused screens that serve no purpose.

Better: Replace the template README with your product's README on day one and delete unused code.

7. What to learn before and after

Suggested order
Naming Things
Types Beside Logic
Project Structure
Following Existing Patterns

Final quick revision

CheckYes / No
Logic only in src/lib/<module>Yes
Each unit has its own folder and types fileYes
Two naming conventions in one folderNo
A utils dumping groundNo
Every variable name in .env.exampleYes
Template README and unused codeDelete