Nest CLI is a command line interface for creating the following things
NestJS CLI command options
- Create a NestJS Application
- update the NestJS application
Prerequisite:
First, Check nest CLI is installed or not by issuing thenest --version command
It gives the installed nestjs version as given below
A:\work>nest --version
9.1.4
If it gives ’nest’ is not recognized as an internal or external command error. It is not installed, So follow to install nestjs cli
A:\work>nest --version
'nest' is not recognized as an internal or external command,
operable program or batch file.
Nestjs command syntax is
nest options
options:
new
or n
: Generates new application
generate
or g
: Generate components such as classes, controllers, services, etc.
How to Generate NestJS application
Let’s create a NestJS application using the nest new command
nest new applicationname
or
nest n application name
applicationname is an application or project name. It creates a folder in File System on the Machine.
With this command, It creates and does the following things.
- Create an Application with the directory structure
- Create a configuration with all dependencies
- Install all dependencies
Here is an example
A:\work>nest new nestjs-hello-world
⚡ We will scaffold your app in a few seconds..
CREATE nestjs-hello-world/.eslintrc.js (665 bytes)
CREATE nestjs-hello-world/.prettierrc (51 bytes)
CREATE nestjs-hello-world/nest-cli.json (118 bytes)
CREATE nestjs-hello-world/package.json (2003 bytes)
CREATE nestjs-hello-world/README.md (3340 bytes)
CREATE nestjs-hello-world/tsconfig.build.json (97 bytes)
CREATE nestjs-hello-world/tsconfig.json (546 bytes)
CREATE nestjs-hello-world/src/app.controller.spec.ts (617 bytes)
CREATE nestjs-hello-world/src/app.controller.ts (274 bytes)
CREATE nestjs-hello-world/src/app.module.ts (249 bytes)
CREATE nestjs-hello-world/src/app.service.ts (142 bytes)
CREATE nestjs-hello-world/src/main.ts (208 bytes)
CREATE nestjs-hello-world/test/app.e2e-spec.ts (630 bytes)
CREATE nestjs-hello-world/test/jest-e2e.json (183 bytes)
? Which package manager would you ❤️ to use? yarn
√ Installation in progress... ☕
🚀 Successfully created project nestjs-hello-world
👉 Get started with the following commands:
$ cd nestjs-hello-world
$ yarn run start
Thanks for installing Nest 🙏
Please consider donating to our open collective
to help us maintain this package.
🍷 Donate: https://opencollective.com/nest
Create Module using nestjs cli command
NestJS is a module design with components. Syntax for modules
nest g mo modulename
nest generate module modulename
Here is an example of generating a module in NestJS.
A:\work\nestjs-hello-world>nest generate module employee
CREATE src/employee/employee.module.ts (85 bytes)
UPDATE src/app.module.ts (326 bytes)
It generates an Empty Typescript file
import { Module } from '@nestjs/common';
@Module({})
export class EmployeeModule {}
Update this module in the import section of the main module(app.module.ts)
import { Module } from '@nestjs/common';
import { EmployeeModule } from './employee/employee.module';
@Module({
imports: [EmployeeModule],
controllers: [],
providers: [],
})
export class AppModule { }
How to create DTO classes in NestJS Cli using the command
Dto or entity or typescript classes can be created using the class option.
nest g cl classname
nest generate class classname
Here is an example
A:\work\nestjs-hello-world>nest g class Employee
CREATE src/employee.ts (25 bytes)
CREATE src/employee.spec.ts (155 bytes)
It creates a typescript class and test file.
How to create a nest json configuration file using the command line
To create a nest-cli.json file, use the configuration
or config
option
A:\work\nestjs-hello-world>nest generate configuration
CREATE nest-cli.json (118 bytes)
```
## Create a Controller using the nest CLI command
The controller is a basic component that takes requests, processes, and returns responses.
```bash
nest g co controllername
nest generate controller controllername
```
Here is an example
```bash
A:\work\nestjs-hello-world>nest generate co Employee
CREATE src/employee/employee.controller.ts (105 bytes)
CREATE src/employee/employee.controller.spec.ts (506 bytes)
UPDATE src/employee/employee.module.ts (182 bytes)
```
It creates a controller and test spec file.
```typescript
import { Controller } from '@nestjs/common';
@Controller('employee')
export class EmployeeController {}
```
Also, update the controller section in employee.module.ts
```typescript
import { Module } from '@nestjs/common';
import { EmployeeController } from './employee.controller';
@Module({
controllers: [EmployeeController]
})
export class EmployeeModule {}
```
## Create service with nests CLI command
```typescript
A:\work\nestjs-hello-world>nest generate service Employee
CREATE src/employee/employee.service.ts (92 bytes)
CREATE src/employee/employee.service.spec.ts (474 bytes)
UPDATE src/employee/employee.module.ts (268 bytes)
```
service and spec file is created
```typescript
import { Injectable } from '@nestjs/common';
@Injectable()
export class EmployeeService {}
```
Also, updated this service file in the Module file(employee.module.ts)
```typescript
import { Module } from '@nestjs/common';
import { EmployeeController } from './employee.controller';
import { EmployeeService } from './employee.service';
@Module({
controllers: [EmployeeController],
providers: [EmployeeService]
})
export class EmployeeModule {}
```
## NestJS Cli command
Following are different options for NestJS CLI commands
┌───────────────┬─────────────┬──────────────────────────────────────────────┐
│ name │ alias │ description │
│ application │ application │ Generate a new application workspace │
│ class │ cl │ Generate a new class │
│ configuration │ config │ Generate a CLI configuration file │
│ controller │ co │ Generate a controller declaration │
│ decorator │ d │ Generate a custom decorator │
│ filter │ f │ Generate a filter declaration │
│ gateway │ ga │ Generate a gateway declaration │
│ guard │ gu │ Generate a guard declaration │
│ interceptor │ itc │ Generate an interceptor declaration │
│ interface │ itf │ Generate an interface │
│ middleware │ mi │ Generate a middleware declaration │
│ module │ mo │ Generate a module declaration │
│ pipe │ pi │ Generate a pipe declaration │
│ provider │ pr │ Generate a provider declaration │
│ resolver │ r │ Generate a GraphQL resolver declaration │
│ service │ s │ Generate a service declaration │
│ library │ lib │ Generate a new library within a monorepo │
│ sub-app │ app │ Generate a new application within a monorepo │
│ resource │ res │ Generate a new CRUD resource │
└───────────────┴─────────────┴──────────────────────────────────────────────┘