How To Install And Setup First NestJS Application?
Last Updated :
12 Jul, 2024
NestJS is a progressive Node.js framework that simplifies the creation of server-side applications. Whether you're building REST APIs, microservices, or GraphQL applications, NestJS offers a structured and modern approach to development. This article will guide you through the installation and initial setup of a NestJS project.
Prerequisites
Steps to Install and Setup NestJS application
Step 1: Install the Nest CLI
The Nest CLI provides an efficient way to create and manage NestJS projects. To install the CLI globally, run the following command in your terminal:
npm install -g @nestjs/cli
You can verify the installation by checking the CLI version:
nest --version
Step 2: Create a New Project
To create a new NestJS project, use the Nest CLI:
nest new nest-gfg
The CLI will prompt you to choose a package manager (npm or yarn) for installing dependencies. After making your choice, the CLI sets up the project structure and installs the necessary packages.
Navigate into your project directory:
cd project-name
Folder Structure
NestJS Folder StructureDependencies
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
}
Key Files and Directories
- src: Contains the source code of your application.
- app.controller.ts: Defines a basic controller.
- app.module.ts: The root module of your application.
- app.service.ts: A basic service.
- main.ts: The entry point of your application.
- test: Contains test files.
- package.json: Lists the dependencies and scripts for your project.
- tsconfig.json: TypeScript configuration file.
- nest-cli.json: Nest CLI configuration file.
Example: Here is a basic NestJS application
JavaScript
//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();
}
}
JavaScript
//app.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello, NestJS!';
}
}
JavaScript
//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 { }
JavaScript
//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();
Step 3: Run the Application
To start the application, use the following command:
npm run start
By default, the application runs on 'http://localhost:3000'.
You can also run the application in development mode with live reloading:
npm run start:dev
This mode watches for file changes and automatically restarts the server.
Output
How To Install And Setup First NestJS ApplicationAdditional Tips and Best Practices
1. Use TypeScript Features: Use TypeScript’s static typing and interfaces for more robust code.
2. Organize Your Code: Keep your code modular by creating separate modules for different features or domains.
3. Use DTOs: Use Data Transfer Objects (DTOs) to validate and transform incoming request data.
4. Enable CORS: Enable Cross-Origin Resource Sharing (CORS) if your application needs to interact with clients from different origins.
5. Security: Use guards and interceptors to handle authentication and authorization.
Similar Reads
How to Install and Creating First Nuxt.js App ?
What is NuxtJS? NuxtJS is a framework of VueJS for creating web apps. It makes development more fast, easy, and organized. NuxtJS is similar to Next.js, which is a framework of React.js. The main features of NuxtJS are: Great folder structure: Nuxt App comes with a great folder structure that makes
2 min read
How to create an application in ember.js ?
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which are based on Model-View-Controller (MVC). Ember is designed for reducing development time and increasing productivity, it is one of the fastest-growing front-end application frameworks being
2 min read
How to Structure my Application in Express.js ?
A clean and well-organized folder structure is crucial for building maintainable and scalable Express.js applications. This article explores best practices for structuring your application, organizing components, and maintaining a modular architecture. Why Structure an Express Application?A well-str
6 min read
Steps to Create an Express.js Application
Creating an Express.js application involves several steps that guide you through setting up a basic server to handle complex routes and middleware. Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Hereâs a
10 min read
How to run a node.js application permanently ?
NodeJS is a runtime environment on a V8 engine for executing JavaScript code with some additional functionality that allows the development of fast and scalable web applications but we can't run the Node.js application locally after closing the terminal or Application, to run the nodeJS application
2 min read
How to Download and Install Node.js and NPM
NodeJS and NPM (Node Package Manager) are essential tools for modern web development. NodeJS is the runtime environment for JavaScript that allows you to run JavaScript outside the browser, while NPM is the package manager that helps manage libraries and code packages in your projects. To run a Node
3 min read
How to Connect Node.js Application to MySQL ?
To connect the Node App to the MySQL database we can utilize the mysql package from Node Package Manager. This module provides pre-defined methods to create connections, query execution and perform other database related operations. Approach to Connect Node App to MySQLFirst, initialize the node.js
2 min read
How to add ESLint in Next.js ?
ESLint is a popular linting tool for identifying and fixing issues in JavaScript code. Adding ESLint to your Next.js project ensures that your code follows best practices and maintains a consistent style. This enhances code quality, helps catch errors early, and makes the codebase easier to maintain
3 min read
How to Install an NPM Package Directly from GitHub ?
Installing npm packages directly from GitHub can be incredibly useful, especially when you need to use a specific version or branch of a package that may not yet be published to the npm registry. This approach allows developers to take advantage of the latest features, bug fixes, or specific package
2 min read
How to Install Angularjs on MacOS?
AngularJS as the name suggests is a JavaScript-based framework. Being more precise AngularJS is an open-source front-end framework for the web. It is backed and maintained by Google. It has been in the market for a long time now, its initial release dated back to Oct 2010, and its last stable releas
3 min read