cli
NestJS CLI
When to Use This Skill
Use this skill when:
- Creating new NestJS projects
- Generating controllers, services, modules, and other components
- Building and bundling applications
- Working with monorepo/workspace structures
- Running development servers
- Managing project configuration
- Creating libraries for shared code
What is the NestJS CLI?
The NestJS CLI is a command-line interface tool that helps you initialize, develop, and maintain NestJS applications. It provides generators for components, supports workspaces, and handles build configurations.
Installation
Global Installation
npm install -g @nestjs/cli
Using npx
npx @nestjs/cli new my-project
Basic Commands
Create New Project
nest new project-name
Options:
--strict- TypeScript strict mode--package-manager [npm|yarn|pnpm]- Package manager to use--language [TS|JS]- Programming language--skip-git- Skip git repository initialization--skip-install- Skip package installation
Example:
nest new my-app --strict --package-manager pnpm
Generate Resources
nest generate <schematic> <name> [options]
Aliases:
nest g <schematic> <name>
Schematics (Generators)
Application
nest generate application <name>
Creates a new application in a monorepo structure.
Module
nest generate module users
nest g mo users
Creates:
users/users.module.ts
Controller
nest generate controller users
nest g co users
Creates:
users/users.controller.tsusers/users.controller.spec.ts
Options:
--no-spec- Skip test file--flat- Don't create a folder
Service
nest generate service users
nest g s users
Creates:
users/users.service.tsusers/users.service.spec.ts
Resource (Complete CRUD)
nest generate resource users
nest g res users
Prompts:
- Transport layer (REST API, GraphQL, Microservice, WebSockets)
- Generate CRUD entry points?
Creates:
- Module
- Controller
- Service
- Entity
- DTOs (create, update)
- Test files
Example output for REST API:
users/
├── dto/
│ ├── create-user.dto.ts
│ └── update-user.dto.ts
├── entities/
│ └── user.entity.ts
├── users.controller.ts
├── users.controller.spec.ts
├── users.module.ts
├── users.service.ts
└── users.service.spec.ts
Other Schematics
# Class
nest g class users/dto/create-user
# Interface
nest g interface users/interfaces/user
# Guard
nest g guard auth/guards/jwt
# Interceptor
nest g interceptor common/interceptors/logging
# Pipe
nest g pipe common/pipes/validation
# Filter
nest g filter common/filters/http-exception
# Middleware
nest g middleware common/middleware/logger
# Decorator
nest g decorator auth/decorators/user
# Gateway (WebSocket)
nest g gateway events
# Resolver (GraphQL)
nest g resolver users
# Library
nest g library shared
Schematic Options
Common Options
# Skip test files
nest g service users --no-spec
# Flat structure (no folder)
nest g controller users --flat
# Dry run (preview changes)
nest g module users --dry-run
# Specific path
nest g service users/services/user
# Skip import into module
nest g service users --no-import
Example:
nest g resource products --no-spec --flat
Build Commands
Development Build
nest build
Compiles TypeScript to JavaScript in dist/ folder.
Production Build
nest build --webpack
Options:
--watch- Watch mode for development--webpack- Use webpack for bundling--path [path]- Path to tsconfig file--config [path]- Path to nest-cli.json
Watch Mode
nest build --watch
Automatically rebuilds on file changes.
Development Commands
Start Application
nest start
Builds and runs the application.
Development Mode (Watch)
nest start --watch
Watches for changes and restarts automatically.
Alias:
npm run start:dev
Debug Mode
nest start --debug
Starts with Node.js inspector for debugging.
Options:
--debug [port]- Debug on specific port (default: 9229)--watch- Watch mode with debugging
Example:
nest start --debug --watch
Workspaces and Monorepo
Create Workspace
nest new my-workspace
cd my-workspace
nest generate app api
nest generate app admin
Structure:
my-workspace/
├── apps/
│ ├── api/
│ └── admin/
├── libs/
├── nest-cli.json
└── package.json
Generate Library
nest generate library shared
nest g lib common
Creates a shared library in libs/ folder.
Usage in apps:
import { SharedModule } from '@app/shared';
Build Specific Application
nest build api
nest build admin
Start Specific Application
nest start api
nest start admin --watch
Project Configuration
nest-cli.json
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true,
"webpack": true,
"tsConfigPath": "tsconfig.build.json"
},
"generateOptions": {
"spec": false,
"flat": true
}
}
Options:
sourceRoot- Source code root directorycompilerOptions.deleteOutDir- Delete output directory before buildcompilerOptions.webpack- Use webpackgenerateOptions.spec- Generate test files by defaultgenerateOptions.flat- Generate without folder
Monorepo Configuration
{
"projects": {
"api": {
"type": "application",
"root": "apps/api",
"entryFile": "main",
"sourceRoot": "apps/api/src",
"compilerOptions": {
"tsConfigPath": "apps/api/tsconfig.app.json"
}
},
"shared": {
"type": "library",
"root": "libs/shared",
"entryFile": "index",
"sourceRoot": "libs/shared/src",
"compilerOptions": {
"tsConfigPath": "libs/shared/tsconfig.lib.json"
}
}
}
}
Additional Commands
Info
nest info
Displays information about:
- Installed packages
- Node.js version
- Operating system
- NestJS CLI version
Example output:
_ _ _ ___ _____ _____ _ _____
| \ | | | | |_ |/ ___|/ __ \| | |_ _|
| \| | ___ ___ | |_ | |\ `--. | / \/| | | |
| . ` | / _ \/ __|| __| | | `--. \| | | | | |
| |\ || __/\__ \| |_ /\__/ //\__/ /| \__/\| |_____| |_
\_| \_/ \___||___/ \__|\____/ \____/ \____/\_____/\___/
[System Information]
OS Version : macOS
NodeJS Version : v18.12.0
NPM Version : 8.19.2
[Nest CLI]
Nest CLI Version : 9.1.5
[Nest Platform Information]
platform-express version : 9.2.0
schematics version : 9.0.3
common version : 9.2.0
core version : 9.2.0
Add
nest add @nestjs/swagger
Adds a library with its schematic installation script.
Common libraries:
nest add @nestjs/swagger
nest add @nestjs/graphql
nest add @nestjs/mongoose
nest add @nestjs/typeorm
Complete Workflow Examples
New REST API Project
# Create project
nest new my-api --strict
# Navigate to project
cd my-api
# Generate a complete resource
nest g resource users
# Generate authentication module
nest g module auth
nest g service auth
nest g controller auth
nest g guard auth/guards/jwt
# Start development server
npm run start:dev
Monorepo Setup
# Create workspace
nest new my-workspace
# Add applications
cd my-workspace
nest g app api
nest g app admin
# Add shared library
nest g lib database
nest g lib common
# Generate resources in specific app
nest g resource users --project api
nest g resource products --project admin
# Start specific app
nest start api --watch
GraphQL Project
# Create project
nest new graphql-api
# Add GraphQL
cd graphql-api
nest add @nestjs/graphql @nestjs/apollo
# Generate GraphQL resources
nest g resource users --type graphql-code-first
nest g resource posts --type graphql-code-first
# Start development
npm run start:dev
Microservices Project
# Create workspace
nest new microservices-app
# Generate microservices
cd microservices-app
nest g app auth-service
nest g app user-service
nest g app api-gateway
# Generate shared library
nest g lib shared
# Start services
nest start auth-service --watch
nest start user-service --watch
nest start api-gateway --watch
Best Practices
- Use generators - Don't create files manually, use CLI generators
- Consistent structure - Let CLI maintain project structure
- Use workspaces - For microservices or multiple related apps
- Shared libraries - Extract common code into libraries
- Configure defaults - Set
generateOptionsin nest-cli.json - Skip specs when needed - Use
--no-specfor DTOs and entities - Use resource generator - For complete CRUD modules
- Dry run first - Test generators with
--dry-run - Version control - Commit nest-cli.json for team consistency
- Use absolute imports - Configure path aliases for clean imports
CLI Shortcuts
# Shorten commands with aliases
nest g mo users # module
nest g co users # controller
nest g s users # service
nest g res users # resource
nest g gu auth/jwt # guard
nest g in logging # interceptor
nest g pi validation # pipe
nest g fi http-exception # filter
nest g mi logger # middleware
nest g d current-user # decorator
nest g ga events # gateway
nest g r users # resolver
Common Scripts
Add these to package.json:
{
"scripts": {
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
}
}
Troubleshooting
CLI Not Found
# Reinstall globally
npm uninstall -g @nestjs/cli
npm install -g @nestjs/cli
# Or use npx
npx @nestjs/cli new my-project
Import Errors in Monorepo
Check tsconfig.json paths:
{
"compilerOptions": {
"paths": {
"@app/shared": ["libs/shared/src"],
"@app/common": ["libs/common/src"]
}
}
}
Build Errors
# Clean build
rm -rf dist
nest build
# Check TypeScript version
npm list typescript