Where Files Live in a Nest App
Project Structure
Tour the default Nest project layout and the conventions the framework pushes you toward.
What you'll learn
- Identify the role of main.ts, app.module.ts, app.controller.ts, app.service.ts
- Understand .spec.ts colocation
- Read nest-cli.json
A fresh Nest project has more files than a plain Node script, but every one has a clear job. Knowing the layout makes the rest of the framework click.
The src/ Tree
src/
main.ts # entry point — creates the app and listens
app.module.ts # root module — composes the app
app.controller.ts # sample HTTP controller
app.controller.spec.ts # unit test for the controller
app.service.ts # sample service (business logic) main.ts is where your app boots:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap(); app.module.ts is the root module — it lists which controllers and
services belong to the app:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
controllers: [AppController],
providers: [AppService],
})
export class AppModule {} Tests Live Next to Their Subject
Unit tests are colocated as *.spec.ts files. End-to-end tests live in a
separate test/ folder at the project root, with their own Jest config so
they can spin up real HTTP servers.
test/
app.e2e-spec.ts
jest-e2e.json nest-cli.json
This file tells the CLI where source lives, which assets to copy at build time, and which schematics defaults to use. The defaults are sane — you rarely edit it until you split into a monorepo.
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
} Scripts in package.json
npm run start:dev, npm test, npm run test:e2e, npm run build. They
all delegate to the Nest CLI or Jest with the right flags.