Introduction
Every great application starts with a solid foundation. The Nest CLI is your primary tool for scaffolding projects, generating components, and maintaining consistency across your codebase. Understanding the CLI workflow will save you hours of boilerplate writing.
Key Concepts
- Nest CLI: A command-line tool for generating and managing NestJS projects
- Scaffold: Auto-generated project structure with configuration files
- Bootstrap: The process of initializing and starting the application
- NestFactory: The core class that creates application instances
Real World Context
In production teams, consistency is paramount. The CLI ensures every developer generates modules, services, and controllers with the same structure. This eliminates debates about file naming, folder structure, and boilerplate patterns.
Deep Dive
Installing the CLI
bashnpm i -g @nestjs/cli
Creating a New Project
bashnest new my-api
The CLI prompts you to choose a package manager (npm, yarn, or pnpm) and scaffolds:
my-api/
├── src/
│ ├── app.controller.ts
│ ├── app.module.ts
│ ├── app.service.ts
│ └── main.ts
├── test/
├── nest-cli.json
├── package.json
└── tsconfig.json
Bootstrapping the Application
The entry point is main.ts:
typescriptimport { 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();
Essential CLI Commands
| Command | Description |
|---|---|
nest new <name> | Create a new project |
nest generate module <name> | Generate a module |
nest generate controller <name> | Generate a controller |
nest generate service <name> | Generate a service |
nest build | Compile the application |
nest start --watch | Start in development mode with hot reload |
Common Pitfalls
- Not using
--watchin development: Runningnest startwithout--watchmeans you restart manually on every change. Always usenest start --watchornpm run start:dev. - Installing CLI locally only: Install globally (
npm i -g) for easy access tonestcommands anywhere. Local installation works but requiresnpx nestprefix. - Ignoring the
nest-cli.jsonconfiguration: This file controls compilation, asset copying, and monorepo settings. Misconfiguring it breaks builds.
Best Practices
- Use the shorthand:
nest g mo usersinstead ofnest generate module users - Generate related files together:
nest g resource userscreates module, controller, service, and DTOs - Keep
main.tsminimal: Configuration belongs in modules, not the bootstrap file - Use environment variables for port:
process.env.PORT ?? 3000enables cloud deployment
Summary
The Nest CLI accelerates development by scaffolding projects and generating components with consistent structure. The NestFactory.create() method bootstraps your application by building the dependency graph from AppModule. Master the CLI commands to maintain velocity as your project grows.
Code Examples
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();