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.