0
0
NestjsHow-ToBeginner ยท 3 min read

How to Run a NestJS Project: Step-by-Step Guide

To run a nestjs project, first install dependencies with npm install, then start the server using npm run start. This launches the app on the default port 3000, accessible via http://localhost:3000.
๐Ÿ“

Syntax

Running a NestJS project involves these main commands:

  • npm install: Installs all project dependencies.
  • npm run start: Starts the NestJS server in development mode.
  • npm run start:prod: Starts the server in production mode.

These commands are run in your project folder using a terminal or command prompt.

bash
npm install
npm run start
npm run start:prod
๐Ÿ’ป

Example

This example shows how to run a basic NestJS project after cloning or creating it.

First, open your terminal in the project folder and run npm install to get all needed packages. Then run npm run start to launch the server. You will see a message that the app is listening on port 3000.

Open http://localhost:3000 in your browser to see the app running.

bash
npm install
npm run start
Output
[Nest] 12345 - 06/01/2024, 10:00:00 AM [NestFactory] Starting Nest application... [Nest] 12345 - 06/01/2024, 10:00:01 AM [InstanceLoader] AppModule dependencies initialized [Nest] 12345 - 06/01/2024, 10:00:01 AM [RoutesResolver] AppController {}: +3ms [Nest] 12345 - 06/01/2024, 10:00:01 AM [NestApplication] Nest application successfully started on port 3000
โš ๏ธ

Common Pitfalls

Common mistakes when running a NestJS project include:

  • Not running npm install first, causing missing dependencies errors.
  • Trying to run npm run start outside the project folder.
  • Port 3000 already in use, causing the server to fail to start.
  • Using node main.js directly instead of npm run start, which may miss environment setups.

Always use the provided npm scripts to ensure proper environment and configuration.

bash
Wrong:
node dist/main.js

Right:
npm run start
๐Ÿ“Š

Quick Reference

Summary tips for running a NestJS project:

  • Always run npm install before starting.
  • Use npm run start for development and npm run start:prod for production.
  • Check your terminal for errors if the server does not start.
  • Access your app at http://localhost:3000 by default.
โœ…

Key Takeaways

Run npm install first to install all dependencies.
Use npm run start to launch the NestJS server in development mode.
Access your running app at http://localhost:3000 by default.
Avoid running node main.js directly; use npm scripts instead.
Check for port conflicts if the server fails to start.