Project Structure

A generated Nest project follows a predictable layout centered on src/ with a main.ts entry point.

When you run nest new, you get a predictable folder layout. Knowing what each file does removes the guesswork.

Key files

FilePurpose
src/main.tsEntry point that bootstraps the app.
src/app.module.tsThe root module.
src/app.controller.tsA sample controller.
src/app.service.tsA sample provider.
test/End-to-end tests.
nest-cli.jsonCLI configuration.

As your app grows you add one folder per feature (for example src/users/) containing that feature's module, controller, service, and DTOs.

Example

Example · bash
src/
  main.ts            # bootstrap
  app.module.ts      # root module
  users/
    users.module.ts
    users.controller.ts
    users.service.ts
    dto/
      create-user.dto.ts
test/
nest-cli.json
package.json
tsconfig.json

When to use it

  • A new engineer clones the repo and immediately understands that all application code lives under src/ and compiled output goes to dist/.
  • A DevOps engineer configures the Docker image to copy only dist/ and node_modules, knowing src/ is not needed at runtime.
  • A team enforces the convention that each feature gets its own subfolder under src/ mirroring the module name (e.g., src/orders/).

More examples

Default project layout

Shows the file tree produced by `nest new`, making every file's role predictable for any team member.

Example · bash
my-api/
├── src/
│   ├── app.controller.ts
│   ├── app.module.ts
│   ├── app.service.ts
│   └── main.ts
├── test/
├── tsconfig.json
└── package.json

Feature module subfolder

Illustrates the recommended co-location pattern where one feature owns its module, controller, service, DTOs, and entities.

Example · bash
src/
├── users/
│   ├── dto/
│   │   └── create-user.dto.ts
│   ├── entities/
│   │   └── user.entity.ts
│   ├── users.controller.ts
│   ├── users.module.ts
│   └── users.service.ts
└── app.module.ts

tsconfig decorator settings

Highlights the tsconfig settings NestJS requires: decorator support and the src→dist compilation mapping.

Example · json
{
  "compilerOptions": {
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Discussion

  • Be the first to comment on this lesson.