Setting Up a NestJS Project with CLI: First App and File Structure Explained

Table of Contents

  1. Prerequisites
  2. Installing NestJS CLI
  3. Creating a New Project
  4. Understanding the Project Structure
  5. Running Your First NestJS Server
  6. Exploring Core Files: main.ts, AppModule, AppController, and AppService
  7. Summary and Next Steps

1. Prerequisites

Before we start, make sure you have the following installed:

  • Node.js (LTS version recommended): https://nodejs.org
  • npm or yarn
  • Basic understanding of TypeScript
  • Git (optional but useful)

2. Installing NestJS CLI

NestJS provides a powerful CLI to scaffold and manage projects efficiently.

npm i -g @nestjs/cli

To confirm installation:

nest --version

This CLI helps you:

  • Generate modules, controllers, and services
  • Scaffold a new project
  • Run and build applications
  • Test and lint your code

3. Creating a New Project

To create a new NestJS project:

nest new my-nest-app

Youโ€™ll be prompted to choose a package manager (npm/yarn/pnpm). Pick your preference.

Once done, navigate into the project:

cd my-nest-app

4. Understanding the Project Structure

Hereโ€™s the default project structure:

my-nest-app/
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ app.controller.ts
โ”‚ โ”œโ”€โ”€ app.controller.spec.ts
โ”‚ โ”œโ”€โ”€ app.module.ts
โ”‚ โ”œโ”€โ”€ app.service.ts
โ”‚ โ””โ”€โ”€ main.ts
โ”œโ”€โ”€ test/
โ”‚ โ””โ”€โ”€ app.e2e-spec.ts
โ”œโ”€โ”€ .eslintrc.js
โ”œโ”€โ”€ .prettierrc
โ”œโ”€โ”€ nest-cli.json
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ tsconfig.json
โ””โ”€โ”€ README.md

Key Files:

  • main.ts โ€“ Entry point of the app, bootstraps Nest.
  • app.module.ts โ€“ Root module of the app.
  • app.controller.ts โ€“ Handles HTTP requests.
  • app.service.ts โ€“ Business logic for the controller.

Nest uses modules to group related components, promoting modularity and separation of concerns.


5. Running Your First NestJS Server

To start the development server:

npm run start:dev

This runs your app in development mode with hot-reloading via ts-node-dev.

Now open http://localhost:3000, and you should see:

Hello World!

This message is returned by the default AppController.


6. Exploring Core Files

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();

This is the entry point of your app. It uses the Nest Factory to create and run an instance of your application.


app.module.ts

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

@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

This defines the applicationโ€™s root module using the @Module() decorator.


app.controller.ts

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
return this.appService.getHello();
}
}

It defines a route (/) and maps it to the getHello() method from the AppService.


app.service.ts

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

@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}

Provides a reusable service, injected into the controller.


7. Summary and Next Steps

Weโ€™ve successfully set up a NestJS project using the CLI, explored the project structure, and understood how a simple app runs. This foundational understanding is key to mastering the framework.