Using Nest CLI to Create and Manage Projects
Installing Nest CLI Globally
If you haven’t installed the Nest CLI yet, you can do so globally with the following command:
npm install -g @nestjs/cliCreating a New Project
Creating a new NestJS project is straightforward. Run the following command:
nest new <project-name>Replace <project-name> with the desired name of your project. To enable strict mode, add the --strict parameter:
nest new <project-name> --strictGenerating Components
Once the project is created, you can use the Nest CLI to generate various components such as controllers, services, modules, etc.
Generate a Controller:
nest generate controller <name>Generate a Service:
nest generate service <name>Generate a Module:
nest generate module <name>Generate a Complete CRUD Module:
nest generate resource <name>Updating Nest CLI
To update an existing Nest CLI installation to the latest version, use:
npm install -g @nestjs/cliViewing Available Commands
To see all available Nest CLI commands, run:
nest --helpCreating Custom Commands
NestJS supports creating custom command-line instructions using the @Command and @Option decorators. For example, create a simple command to output “Hello World”:
import { Command, Option } from '@nestjs/cli';
@Command({
command: 'hello',
describe: 'Prints a greeting',
})
export class HelloCommand {
@Option({
flags: '-n, --name <name>',
describe: 'The name to greet',
default: 'World',
})
name: string;
async run() {
console.log(`Hello, ${this.name}!`);
}
}Running Custom Commands
Once a custom command is created, you can run it with:
nest hello -n YourNameOther Common Commands
Check Nest CLI Version:
nest --versionList Installed Packages:
npm listDelete a Project:
To delete a project, navigate to the project directory and run rm -rf . (ensure you back up data first). Alternatively, manually delete the project folder.
Example
Suppose you want to create a new project named my-nest-app and generate a users module, controller, and service.
Create the Project:
nest new my-nest-app
cd my-nest-appGenerate Module, Controller, and Service:
nest generate module users
nest generate controller users
nest generate service usersThese commands will generate the corresponding file structure in your project.
Automatic Code Generation
Generating a Controller
Generate a new controller, e.g., a users controller:
nest generate controller usersGenerating a Service
Generate a new service, e.g., a users service:
nest generate service usersGenerating a Module
Generate a new module, e.g., a users module:
nest generate module usersGenerating a Complete CRUD Module
Generate a complete CRUD structure including a controller, service, and module:
nest generate resource usersGenerating an Entity Class
Generate an entity class, e.g., a User entity:
nest generate entity UserGenerating a DTO Class
Generate a DTO class, e.g., CreateUserDto:
nest generate dto CreateUserDtoGenerating an Interface
Generate an interface, e.g., UsersRepository:
nest generate interface UsersRepositoryGenerating a Pipe
Generate a pipe, e.g., ValidationPipe:
nest generate pipe ValidationPipeGenerating a Guard
Generate a guard, e.g., AuthGuard:
nest generate guard AuthGuardGenerating a Filter
Generate an exception filter, e.g., HttpExceptionFilter:
nest generate exception-filter HttpExceptionFilterGenerating an Interceptor
Generate an interceptor, e.g., LoggingInterceptor:
nest generate interceptor LoggingInterceptorGenerating a Decorator
Generate a decorator, e.g., Roles:
nest generate decorator RolesGenerating an Enum
Generate an enum, e.g., RoleEnum:
nest generate enum RoleEnumGenerating a Configuration File
Generate a configuration file, e.g., database.config:
nest generate config databaseGenerating a Global Configuration File
Generate a global configuration file, e.g., global.config:
nest generate global-configGenerating a Global Pipe
Generate a global pipe, e.g., GlobalValidationPipe:
nest generate global-pipe GlobalValidationPipeGenerating a Global Guard
Generate a global guard, e.g., GlobalAuthGuard:
nest generate global-guard GlobalAuthGuardGenerating a Global Filter
Generate a global exception filter, e.g., GlobalExceptionFilter:
nest generate global-exception-filter GlobalExceptionFilterGenerating a Global Interceptor
Generate a global interceptor, e.g., GlobalLoggingInterceptor:
nest generate global-interceptor GlobalLoggingInterceptorGenerating a Global Decorator
Generate a global decorator, e.g., GlobalRoles:
nest generate global-decorator GlobalRolesGenerating a Global Enum
Generate a global enum, e.g., GlobalRoleEnum:
nest generate global-enum GlobalRoleEnumGenerating a Global Configuration File
Generate a global configuration file, e.g., global.database.config:
nest generate global-config global.databaseExample
Suppose you want to generate a controller, service, and module for a users module.
Generate Module:
nest generate module usersGenerate Controller:
nest generate controller usersGenerate Service:
nest generate service usersThese commands will generate the corresponding file structure in your project.



