NestJS CLI Basics
The NestJS CLI (Command Line Interface) is your daily companion when building NestJS applications. It creates projects, generates code files, and automates repetitive tasks. Instead of manually creating folders and files, you type one command and the CLI handles the rest.
What the CLI Does for You
Imagine a factory that builds car parts on demand. You press a button labeled "door," and a perfectly shaped door rolls out — no cutting, no measuring, no guessing. The NestJS CLI works the same way. You tell it what you need, and it generates correct, ready-to-use code files.
The Main CLI Command: nest generate
The most important CLI command is nest generate, shortened to nest g. You use it to create individual building blocks of your application.
The pattern is always:
nest g [type] [name]
Examples:
nest g module users nest g controller users nest g service users
Each command creates the corresponding file and automatically updates the relevant module to include the new piece.
Types You Can Generate
Command | What It Creates -------------------------|------------------------------------ nest g module [name] | A new module file nest g controller [name] | A controller with route handlers nest g service [name] | A service for business logic nest g guard [name] | An authentication guard nest g pipe [name] | A validation pipe nest g filter [name] | An exception filter nest g interceptor [name]| A request/response interceptor nest g decorator [name] | A custom decorator nest g middleware [name] | An Express-style middleware
The --dry-run Flag
Not sure what a command will create? Add --dry-run to preview the output without actually writing any files:
nest g module products --dry-run
The CLI prints a list of files it would create or update. No actual changes happen. This is useful before running a command for the first time.
Generating a Full Feature at Once
You can generate a module, controller, and service for a feature all at once using the resource command:
nest g resource products
The CLI asks whether you want a REST API or GraphQL resolver. Choose REST API, and it generates all the boilerplate code for a complete CRUD feature — create, read, update, and delete endpoints — automatically.
This is the power of the CLI at its best. A task that would take 15 minutes manually takes 10 seconds with one command.
The nest new Command
You use nest new only once per project — to create the initial project structure:
nest new project-name
After you type this, the CLI:
- Creates a new folder with your project name
- Generates the starter files (main.ts, app.module.ts, app.controller.ts, app.service.ts)
- Installs all npm dependencies automatically
CLI Folder Organization
When you generate a feature called users, the CLI creates files inside a users folder automatically:
src/
└── users/
├── users.module.ts
├── users.controller.ts
├── users.controller.spec.ts ← test file
├── users.service.ts
└── users.service.spec.ts ← test file
The CLI also updates the parent module (usually app.module.ts) to import the new UsersModule. You do not need to do this manually.
nest build and nest start
Two other CLI commands are essential:
npm run start ← Starts the app (production mode) npm run start:dev ← Starts with auto-restart on file changes npm run build ← Compiles TypeScript to JavaScript
During development, always use start:dev. It watches your files and restarts the server whenever you save a change — saving you the step of manually stopping and restarting.
Checking Available Commands
Run nest --help to see a full list of CLI commands:
nest --help
Run nest g --help to see all generate options:
nest g --help
Why the CLI Matters
Every file the CLI generates follows the exact pattern NestJS expects. Decorators are in the right place. Imports are correct. The module registration happens automatically. This consistency removes human error and ensures every file in your project looks and behaves the same way.
Developers who skip the CLI and create files manually often miss a registration step or place a decorator incorrectly. The CLI eliminates these mistakes entirely. Make it a habit to use the CLI for all new file creation.
