Skip to content

NestJS Setup

This lesson walks through installing the tools you need and creating your first real NestJS project using the official Nest CLI, then explains what each generated file does.

Prerequisites and Installation

NestJS requires Node.js (LTS versions are recommended) and works with either npm, yarn, or pnpm. The fastest way to start a new project is the Nest CLI, which scaffolds a working application with sensible defaults for TypeScript, testing, and linting.

npm i -g @nestjs/cli
nest new my-nest-app
cd my-nest-app
npm run start:dev

nest new prompts for a package manager, generates a full project skeleton, and installs dependencies. start:dev runs the app with ts-node and file-watching enabled.

Generated 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
├─ nest-cli.json
├─ tsconfig.json
└─ package.json
  • src/main.ts is the entry point that bootstraps the app.
  • src/app.module.ts is the root module every feature module eventually connects to.
  • test/ holds end-to-end specs, separate from unit test files placed alongside source (*.spec.ts).
  • nest-cli.json configures how the CLI compiles and generates code for this project.

Setup Command Reference

Commands you'll run when starting or running a Nest project.

Command Purpose
npm i -g @nestjs/cli Installs the Nest CLI globally
nest new <name> Scaffolds a new Nest project
npm run start Starts the app once (no watch mode)
npm run start:dev Starts the app with file-watching enabled
npm run build Compiles TypeScript to dist/
npm run start:prod Runs the compiled dist/main.js

Setting Up Without the CLI

You can also add NestJS to an existing TypeScript project by installing the core packages directly. This is less common but useful when integrating Nest into an existing monorepo.

npm install @nestjs/core @nestjs/common @nestjs/platform-express reflect-metadata rxjs
npm install -D typescript @types/node

reflect-metadata and rxjs are required peer dependencies, Nest's dependency injection relies on TypeScript's emitted decorator metadata, and RxJS powers interceptors and microservice transports.

Required tsconfig Options

Nest's decorator-based dependency injection depends on two TypeScript compiler options being enabled, the CLI sets these automatically, but they matter if you configure tsconfig.json by hand.

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "target": "ES2021",
    "module": "commonjs"
  }
}

Common Mistakes

  • Using an old Node.js version that doesn't meet the current Nest engine requirement.
  • Forgetting emitDecoratorMetadata in a hand-rolled tsconfig.json, which silently breaks type-based dependency injection.
  • Running npm run start in production and expecting file-watching, that's what start:dev is for.
  • Committing the generated dist/ folder to version control instead of .gitignore-ing it.

Key Takeaways

  • The Nest CLI (nest new) is the fastest way to scaffold a new project with correct defaults.
  • The generated structure separates the entry file, root module, and end-to-end tests clearly.
  • experimentalDecorators and emitDecoratorMetadata are required TypeScript compiler flags.
  • start:dev is for local development; build plus start:prod is for running compiled code.

Pro Tip

Add a .nvmrc file pinning the Node.js version you scaffolded the project with, teammates (and CI) running a different Node major version are one of the most common sources of "works on my machine" Nest bugs.