NestJS Basics - Generating Components Using the CLI


Introduction to NestJS CLI

  • NestJS CLI Docs
  • some common commands
    • nest g module [name]: generates new module
    • nest g controller [name]: generates new controller
    • nest g service [name]: generates new service

The following sections will assume this directory structure:

app
|-- src/
|---- app.controller.ts
|---- app.module.ts
|---- app.service.ts
|---- main.ts
|-- package.json
|-- tsconfig.json
|-- <other setup files>

Generate A Module

Running this command from the root app directory

nest g module books

will result in the following:

app
|-- src/
+ |---- books/
+ |------ books.module.ts
|---- app.controller.ts
+ |---- app.module.ts <-- will add BooksModule to AppModule as import
|---- app.service.ts
|---- main.ts
|-- package.json
|-- tsconfig.json
|-- <other setup files>

Generate A Controller

Running this command from the root app directory

nest g controller books

will result in the following:

app
|-- src/
|---- books/
+ |------ books.controller.ts
+ |------ books.module.ts <-- will add BooksController to BooksModule configuration
|---- app.controller.ts
|---- app.module.ts
|---- app.service.ts
|---- main.ts
|-- package.json
|-- tsconfig.json
|-- <other setup files>

Generate A Service

Running this command from the root app directory

nest g service books

will result in the following:

app
|-- src/
|---- books/
|------ books.controller.ts
+ |------ books.module.ts <-- will add BooksService to BooksModule configuration
+ |------ books.service.ts
|---- app.controller.ts
|---- app.module.ts
|---- app.service.ts
|---- main.ts
|-- package.json
|-- tsconfig.json
|-- <other setup files>

Generated Files

books.module.ts

import { Module } from '@nestjs/common';
import { BooksController } from './books.controller';
import { BooksService } from './books.service';

@Module({
  controllers: [BooksController],
  providers: [BooksService],
})
export class BooksModule {}

books.controller.ts

import { Controller, Get } from '@nestjs/common';
import { BooksService } from './books.service';

@Controller('books')
export class BooksController {}

books.service.ts

import { Injectable } from '@nestjs/common';

@Injectable()
export class BooksService {
  findAll(): string {}
}

app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { BooksModule } from './books/books.module';

@Module({
  imports: [BooksModule], // this was added `nest g module books` command
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
Made with Gatsby G Logo