How to Install NestJS Framework: A Complete Guide
NestJS is a modern, progressive Node.js framework for building efficient and scalable server-side applications. It uses TypeScript by default, provides out-of-the-box support for dependency injection, modular architecture, and much more. This tutorial will guide you step-by-step on how to install NestJS and get started with your first project.
Prerequisites
- Node.js and npm: Ensure you have Node.js (version 14 or later) and npm installed. You can download them from the official Node.js website.
- Basic knowledge of TypeScript and JavaScript will help you understand NestJS concepts better.
- A code editor like Visual Studio Code is recommended for development.
Step 1: Install NestJS CLI
The easiest way to start with NestJS is using its Command Line Interface (CLI) tool. It helps scaffold projects and generate components quickly.
npm i -g @nestjs/cli
This installs the Nest CLI globally on your system.
Step 2: Create a New NestJS Project
Use the CLI to create a new project by running:
nest new my-nest-project
Replace my-nest-project with your desired project folder name.
The CLI will prompt you to choose a package manager (npm or yarn). Select your preference and allow the tool to install dependencies.
Step 3: Understand the Project Structure
The scaffolded project includes essential folders and files:
src/: Main source folder containing app modules, controllers, and services.main.ts: The entry point bootstrap file for the app.package.json: Manages dependencies and scripts.
Step 4: Run the Application
Navigate to your project folder and start the development server with:
npm run start:dev
This command runs the app in watch mode, so it reloads on file changes.
Open http://localhost:3000 in your browser; you should see a default welcome message confirming NestJS is installed correctly.
Step 5: Optional – Install Additional Dependencies
Depending on your project needs, you might want to install extra packages, such as:
- TypeORM or Mongoose: For database integration.
- Passport: For authentication.
- Swagger: For API documentation.
For example, to install TypeORM and the PostgreSQL driver:
npm install --save @nestjs/typeorm typeorm pg
Troubleshooting Tips
- If
nestcommand is not recognized, ensure your npm global bin folder is in your system PATH. - Make sure Node.js and npm versions are up to date and compatible with NestJS requirements.
- Run commands with appropriate permissions (try using
sudoon Linux/macOS if permission denied). - Check your internet connection when installing dependencies.
- If problem persists, consult the official NestJS documentation for guidance (Official site).
Summary Checklist
- Installed Node.js and npm.
- Installed NestJS CLI globally.
- Created a new NestJS project with
nest new. - Run
npm run start:devto start the application. - Verified the app runs at
http://localhost:3000. - Installed any additional dependencies as needed.
For more backend development tutorials, check out our guide on <a href="/
