Getting Started
Getting a NestJS project running takes less than a minute thanks to the official CLI. This guide walks through installing the tooling, scaffolding a project, understanding what the CLI generates, and using code generators to stay productive.
Prerequisites
You need Node.js 18 or later and a package manager (npm, pnpm, or yarn). Verify your environment first:
node --version
npm --version
Installing the Nest CLI
The @nestjs/cli package provides the nest command used to scaffold, generate, and build projects. Install it globally:
npm install -g @nestjs/cli
nest --version
Prefer not to install globally? Use
npx @nestjs/cli new my-appand the CLI runs without a global install.
Scaffolding a new project
nest new generates a fully wired starter with TypeScript, testing, linting, and formatting already configured.
nest new my-app
The CLI prompts you to pick a package manager, then installs dependencies. When it finishes you have a runnable application.
cd my-app
npm run start:dev
Output:
[Nest] LOG [NestFactory] Starting Nest application...
[Nest] LOG [NestApplication] Nest application successfully started
Application is running on: http://localhost:3000
Project structure
The generated layout is intentionally minimal. Everything lives under src/, with tests colocated and tooling configs at the root.
my-app/
├── src/
│ ├── app.controller.ts # A basic controller
│ ├── app.controller.spec.ts # Unit test for the controller
│ ├── app.service.ts # A basic provider (service)
│ ├── app.module.ts # The root module
│ └── main.ts # Entry point — bootstraps the app
├── test/ # End-to-end tests
├── nest-cli.json # CLI configuration
├── tsconfig.json
└── package.json
The entry point, main.ts, creates the application instance from the root module and starts listening:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
Generating code with the CLI
Hand-writing boilerplate is error-prone. The nest generate command (aliased nest g) scaffolds components and automatically registers them in the nearest module.
# Generate a feature module
nest g module users
# Generate a controller and service in that module
nest g controller users
nest g service users
The CLI updates users.module.ts for you, adding the controller and provider to the metadata. To scaffold a complete CRUD resource, module, controller, service, DTOs, and entity, in one command, use the resource schematic:
nest g resource users
| Command | Alias | Creates |
|---|---|---|
nest g module <name> | nest g mo | A module |
nest g controller <name> | nest g co | A controller + spec |
nest g service <name> | nest g s | A service + spec |
nest g resource <name> | nest g res | Full CRUD scaffold |
Pass
--dry-run(or-d) to preview the files a generator will create and modify without touching disk. It is the safest way to learn what a schematic does.
Running in development
Use the watch-mode script during development, it recompiles and restarts on every file change:
npm run start:dev
For production builds, compile ahead of time and run the JavaScript output:
npm run build
npm run start:prod
With the project scaffolded and running, you are ready to build out controllers, providers, and modules, the three core building blocks covered next.