Lesson 2 / 34

Setup with NestJS CLI

Scaffold a NestJS project with the Nest CLI and tour main.ts, the root module and the sample controller and service.

Create a project

The @nestjs/cli package generates everything—package.json, tsconfig, and starter files.

npm install -g @nestjs/cli
nest new my-app
cd my-app
npm run start:dev

Generated files

You get a src/ folder with main.ts (bootstrap), app.module.ts (root module), and app.controller.ts + app.service.ts (example controller/service pair).

Generate a full CRUD resource

nest g resource users creates a module, controller, service, DTOs and an entity in one go.

  • g is short for generate.
  • --no-spec skips the test files.
  • The CLI also registers UsersModule in AppModule for you.
nest g resource users --no-spec

Use --dry-run first

Add --dry-run to any nest g command to preview which files will be created or changed. Nothing is written to disk, so it is safe to experiment.

Quick check

Quick check: What does `nest g resource users` do that `nest g controller users` does not?

  • It only creates a service file
  • It generates module, controller, service and DTOs together
  • It starts the server in watch mode
Answer

It generates module, controller, service and DTOs together — `resource` scaffolds the whole feature slice, while `controller` creates only the controller.