API Reference

NestJS

Progressive Node.js framework with TypeScript decorators, dependency injection modules, guards, pipes, and first-class testing support.

Official Documentation ↗
CompatibilityThis documentation covers version 12.3.3. Check the official docs for the latest API surface.

Installation

npmnpm install @nestjs/core @nestjs/common @nestjs/platform-express reflect-metadata rxjs
pnpmpnpm add @nestjs/core @nestjs/common @nestjs/platform-express reflect-metadata rxjs
yarnyarn add @nestjs/core @nestjs/common @nestjs/platform-express reflect-metadata rxjs

Methods

NestJS organizes server code around modules, controllers, and providers. TypeScript decorators declare routes and injection points, while the runtime container wires dependencies, so application structure stays consistent as the codebase grows.

Overview

A Nest application starts from a root module. Modules group controllers that handle HTTP requests and providers that hold business logic. Request data passes through guards, interceptors, pipes, and exception filters in a defined order, which gives cross-cutting concerns a single place to live. The framework runs on Express by default and can switch to Fastify.

Installation

Install @nestjs/core, @nestjs/common, and a platform adapter such as @nestjs/platform-express, plus reflect-metadata and rxjs. Enable experimentalDecorators and emitDecoratorMetadata in tsconfig.json. Validation needs class-validator and class-transformer. The Nest CLI can scaffold a project with npx @nestjs/cli new, but the runtime packages are enough for an existing codebase.

Core API walkthrough

NestFactory.create bootstraps the root module and returns the application. @Controller sets a route prefix, and method decorators such as @Get map handlers to verbs. @Injectable registers providers with the container. @Module declares controllers, providers, and exports. ValidationPipe validates and transforms DTOs before the handler runs, removing manual type checks from controllers.

Pitfalls

Providers are not global: a service must be exported by its module and that module imported elsewhere. Global pipes apply to every route, so validation errors can change existing API contracts. Using any in DTOs defeats validation. Also avoid business logic in controllers, and remember that exception filters must be registered to shape error responses consistently.

Verification

Boot the app and request each route with curl or Supertest. Confirm invalid payloads return 400 with field details, and that an unknown id returns 404 from the service. Use Test.createTestingModule to unit test providers with mocked dependencies, and check that guards reject unauthenticated requests before the handler executes.

FAQ

Do I need the Nest CLI to use NestJS?
No. NestFactory and the decorators work in any TypeScript project. The CLI mainly scaffolds files, runs builds, and generates resources, which is convenient but optional.
Why does dependency injection fail for my service?
The provider is usually missing from the module providers array, or the class lacks @Injectable. Providers are module-scoped, so another module must import the exporting module rather than the class file directly.
How do I test a controller without booting the app?
Use Test.createTestingModule from @nestjs/testing, override the service provider with a mock, compile the module, and call the controller methods directly or through supertest.