Lesson 31-NestJS CLI Tool Usage

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/cli

Creating 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> --strict

Generating 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/cli

Viewing Available Commands

To see all available Nest CLI commands, run:

nest --help

Creating 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 YourName

Other Common Commands

Check Nest CLI Version:

nest --version

List Installed Packages:

npm list

Delete 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-app

Generate Module, Controller, and Service:

nest generate module users
nest generate controller users
nest generate service users

These 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 users

Generating a Service

Generate a new service, e.g., a users service:

nest generate service users

Generating a Module

Generate a new module, e.g., a users module:

nest generate module users

Generating a Complete CRUD Module

Generate a complete CRUD structure including a controller, service, and module:

nest generate resource users

Generating an Entity Class

Generate an entity class, e.g., a User entity:

nest generate entity User

Generating a DTO Class

Generate a DTO class, e.g., CreateUserDto:

nest generate dto CreateUserDto

Generating an Interface

Generate an interface, e.g., UsersRepository:

nest generate interface UsersRepository

Generating a Pipe

Generate a pipe, e.g., ValidationPipe:

nest generate pipe ValidationPipe

Generating a Guard

Generate a guard, e.g., AuthGuard:

nest generate guard AuthGuard

Generating a Filter

Generate an exception filter, e.g., HttpExceptionFilter:

nest generate exception-filter HttpExceptionFilter

Generating an Interceptor

Generate an interceptor, e.g., LoggingInterceptor:

nest generate interceptor LoggingInterceptor

Generating a Decorator

Generate a decorator, e.g., Roles:

nest generate decorator Roles

Generating an Enum

Generate an enum, e.g., RoleEnum:

nest generate enum RoleEnum

Generating a Configuration File

Generate a configuration file, e.g., database.config:

nest generate config database

Generating a Global Configuration File

Generate a global configuration file, e.g., global.config:

nest generate global-config

Generating a Global Pipe

Generate a global pipe, e.g., GlobalValidationPipe:

nest generate global-pipe GlobalValidationPipe

Generating a Global Guard

Generate a global guard, e.g., GlobalAuthGuard:

nest generate global-guard GlobalAuthGuard

Generating a Global Filter

Generate a global exception filter, e.g., GlobalExceptionFilter:

nest generate global-exception-filter GlobalExceptionFilter

Generating a Global Interceptor

Generate a global interceptor, e.g., GlobalLoggingInterceptor:

nest generate global-interceptor GlobalLoggingInterceptor

Generating a Global Decorator

Generate a global decorator, e.g., GlobalRoles:

nest generate global-decorator GlobalRoles

Generating a Global Enum

Generate a global enum, e.g., GlobalRoleEnum:

nest generate global-enum GlobalRoleEnum

Generating a Global Configuration File

Generate a global configuration file, e.g., global.database.config:

nest generate global-config global.database

Example
Suppose you want to generate a controller, service, and module for a users module.

Generate Module:

nest generate module users

Generate Controller:

nest generate controller users

Generate Service:

nest generate service users

These commands will generate the corresponding file structure in your project.

Share your love