Skip to content

Next.js Folder Structure

“Code is read more often than it is written.” — Guido van Rossum

A well-organized folder structure:

  • Makes onboarding new developers faster
  • Reduces bugs from misplaced files
  • Enables better team collaboration
  • Scales gracefully from small to large projects
PrincipleDescription
Separation of ConcernsUI, business logic, and data layers separated
Feature CohesionRelated files grouped together
Single ResponsibilityEach file/folder has one clear purpose
DiscoverabilityA new developer can find anything intuitively

For small projects and learning:

my-app/
├── app/
│ ├── layout.tsx ← Root layout
│ ├── page.tsx ← Home page (/)
│ ├── about/
│ │ └── page.tsx ← About page (/about)
│ └── globals.css
├── components/
│ ├── Navbar.tsx
│ ├── Footer.tsx
│ └── Button.tsx
├── public/
│ └── images/
│ └── logo.png
├── next.config.ts
├── package.json
└── tsconfig.json

For real applications:

my-app/
├── app/
│ ├── (auth)/
│ │ ├── login/page.tsx
│ │ └── register/page.tsx
│ ├── (dashboard)/
│ │ ├── layout.tsx
│ │ ├── page.tsx
│ │ └── settings/page.tsx
│ ├── api/
│ │ └── users/
│ │ └── route.ts
│ ├── layout.tsx
│ └── page.tsx
├── components/
│ ├── ui/
│ │ ├── Button.tsx
│ │ ├── Input.tsx
│ │ └── Modal.tsx
│ └── shared/
│ ├── Navbar.tsx
│ └── Footer.tsx
├── lib/
│ ├── db.ts ← Database connection
│ └── auth.ts ← Auth utilities
├── hooks/
│ ├── useAuth.ts
│ └── useDebounce.ts
├── types/
│ └── index.ts ← TypeScript interfaces
├── utils/
│ ├── format.ts
│ └── validation.ts
└── public/
├── images/
└── fonts/

For large teams and enterprise applications:

my-app/
├── app/
│ ├── (auth)/
│ │ ├── layout.tsx
│ │ ├── login/
│ │ │ ├── page.tsx
│ │ │ └── loading.tsx
│ │ └── register/
│ │ └── page.tsx
│ ├── (dashboard)/
│ │ ├── layout.tsx
│ │ ├── page.tsx
│ │ ├── analytics/
│ │ │ └── page.tsx
│ │ └── settings/
│ │ └── page.tsx
│ ├── api/
│ │ ├── auth/
│ │ │ └── route.ts
│ │ ├── users/
│ │ │ └── route.ts
│ │ └── products/
│ │ └── route.ts
│ ├── error.tsx
│ ├── not-found.tsx
│ ├── layout.tsx
│ └── page.tsx
├── components/
│ ├── ui/ ← Reusable design system components
│ │ ├── Button/
│ │ │ ├── Button.tsx
│ │ │ ├── Button.test.tsx
│ │ │ └── index.ts
│ │ ├── Input/
│ │ └── Card/
│ ├── forms/ ← Form-specific components
│ │ ├── LoginForm.tsx
│ │ └── ProductForm.tsx
│ └── layouts/ ← Layout components
│ ├── DashboardLayout.tsx
│ └── AuthLayout.tsx
├── features/ ← Feature-based organization
│ ├── auth/
│ │ ├── components/
│ │ ├── hooks/
│ │ ├── actions.ts
│ │ └── types.ts
│ ├── products/
│ │ ├── components/
│ │ ├── hooks/
│ │ └── types.ts
│ └── users/
│ ├── components/
│ └── types.ts
├── lib/
│ ├── db/
│ │ ├── index.ts ← Database client (Prisma/Drizzle)
│ │ └── schema.ts
│ ├── auth/
│ │ └── index.ts
│ └── email/
│ └── index.ts
├── hooks/
│ ├── useAuth.ts
│ ├── useLocalStorage.ts
│ └── useDebounce.ts
├── services/ ← External API integrations
│ ├── stripe.ts
│ ├── sendgrid.ts
│ └── cloudinary.ts
├── store/ ← Global state (Zustand/Redux)
│ ├── index.ts
│ └── slices/
│ └── authSlice.ts
├── types/
│ ├── index.ts ← Shared TypeScript types
│ ├── api.ts
│ └── db.ts
├── utils/
│ ├── format.ts
│ ├── validation.ts
│ └── helpers.ts
├── config/
│ ├── constants.ts
│ └── env.ts
├── middleware.ts ← Next.js Middleware
└── public/
├── images/
├── icons/
└── fonts/

FolderPurposeContains
app/Next.js App RouterPages, layouts, API routes
components/Reusable UIReact components
lib/Core utilitiesDB, auth, third-party setup
hooks/Custom React hooksuseXxx.ts files
utils/Pure functionsFormatters, validators
services/External APIsStripe, email, storage
types/TypeScript typesInterfaces, enums
store/Global stateZustand/Redux slices
config/App configurationConstants, env vars
features/Feature modulesSelf-contained feature code
public/Static assetsImages, fonts, icons
middleware.tsEdge middlewareAuth guards, redirects

SVG: Folder Relationships diagram


ecommerce/
├── app/
│ ├── (shop)/
│ │ ├── page.tsx ← Home / product listing
│ │ ├── products/
│ │ │ ├── page.tsx ← All products
│ │ │ └── [slug]/page.tsx ← Product detail
│ │ └── cart/page.tsx ← Shopping cart
│ ├── (checkout)/
│ │ ├── layout.tsx
│ │ └── page.tsx
│ ├── (account)/
│ │ ├── orders/page.tsx
│ │ └── profile/page.tsx
│ └── api/
│ ├── products/route.ts
│ ├── orders/route.ts
│ └── webhooks/stripe/route.ts
├── features/
│ ├── cart/
│ ├── checkout/
│ └── products/
└── services/
├── stripe.ts
└── inventory.ts
saas-app/
├── app/
│ ├── (marketing)/ ← Public pages
│ │ ├── page.tsx
│ │ └── pricing/page.tsx
│ ├── (app)/ ← Authenticated app
│ │ ├── layout.tsx ← Dashboard layout
│ │ ├── dashboard/page.tsx
│ │ ├── projects/
│ │ └── team/
│ └── api/
│ └── webhooks/
├── features/
│ ├── projects/
│ ├── billing/
│ └── team/
└── lib/
├── db/
└── stripe/
blog/
├── app/
│ ├── page.tsx ← Blog listing
│ ├── posts/
│ │ └── [slug]/
│ │ ├── page.tsx ← Blog post
│ │ └── loading.tsx
│ ├── categories/
│ │ └── [category]/page.tsx
│ └── feed.xml/route.ts ← RSS feed
├── lib/
│ └── mdx.ts ← MDX processing
└── content/
└── posts/
└── hello-world.mdx

ItemConventionExample
ComponentsPascalCaseUserCard.tsx
HookscamelCase with use prefixuseAuth.ts
UtilitiescamelCaseformatDate.ts
Types/InterfacesPascalCaseUserProfile
ConstantsUPPER_SNAKE_CASEMAX_RETRIES
API routeslowercaseroute.ts
Page fileslowercasepage.tsx
Folderslowercase with hyphensuser-profile/

❌ Putting all components in one giant file
❌ Mixing UI logic with business logic
❌ Importing directly from deep nested paths (use barrel exports)
❌ Keeping API keys in component files (use environment variables)
❌ Not separating server and client components
❌ Creating circular imports between modules
✅ Use @/ alias instead of ../../.. relative paths
✅ Create index.ts barrel files for clean imports

Beginner:

  1. What is the purpose of the public/ folder in Next.js?
  2. What goes inside the lib/ folder vs utils/ folder?
  3. Where should TypeScript interfaces be stored?

Intermediate: 4. What is feature-based architecture and when should you use it? 5. How do barrel files (index.ts) improve imports in large projects? 6. Explain the difference between components/ui/ and components/features/.

Advanced: 7. How would you structure a monorepo with multiple Next.js apps sharing components? 8. Describe the Clean Architecture pattern and how it applies to Next.js.