{"id":11390,"library":"nestjs","title":"NestJS - Progressive Node.js Framework","description":"NestJS is a progressive, open-source Node.js framework for building efficient, reliable, and scalable server-side applications, released under an MIT License. It leverages TypeScript extensively, combining elements of Object-Oriented Programming (OOP), Functional Programming (FP), and Functional Reactive Programming (FRP) to provide a structured and opinionated development experience. Built on top of robust HTTP server frameworks like Express (default) and Fastify, it provides an out-of-the-box application architecture that helps developers create highly testable, loosely coupled, and easily maintainable applications. The current stable version is 11.1.19, with regular releases bringing new features, bug fixes, and performance enhancements. Key differentiators include its strong adherence to Angular-inspired modularity, dependency injection system, and a rich ecosystem for various application types like REST APIs, GraphQL APIs, and microservices.","status":"active","version":"0.0.1","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","web","framework"],"install":[{"cmd":"npm install nestjs","lang":"bash","label":"npm"},{"cmd":"yarn add nestjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add nestjs","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Default HTTP server platform, often implicitly used.","package":"@nestjs/platform-express","optional":true},{"reason":"Required for decorators and TypeScript-based dependency injection. Must be imported once at the application's entry point.","package":"reflect-metadata","optional":false},{"reason":"Used for reactive programming patterns, especially with Interceptors and Guards.","package":"rxjs","optional":true}],"imports":[{"note":"Used to create a NestJS application instance, typically in main.ts. The core functionality is separate from common utilities.","wrong":"import { NestFactory } from 'nestjs';","symbol":"NestFactory","correct":"import { NestFactory } from '@nestjs/core';"},{"note":"The `@Module()` decorator is from `@nestjs/common`, not `@nestjs/core`. It defines modules, controllers, and providers.","wrong":"import { Module } from '@nestjs/core';","symbol":"Module","correct":"import { Module } from '@nestjs/common';"},{"note":"All fundamental decorators like `@Controller()`, `@Get()`, `@Post()`, `@Injectable()`, etc., are exported from `@nestjs/common`.","wrong":"import { Controller } from '@nestjs/core'; // And other decorators","symbol":"Controller, Get, Post","correct":"import { Controller, Get, Post } from '@nestjs/common';"}],"quickstart":{"code":"import { NestFactory } from '@nestjs/core';\nimport { Module, Controller, Get, Post, Body, Param, HttpStatus, HttpException, Injectable } from '@nestjs/common';\n\n// 1. Define a Service (Provider)\n@Injectable()\nclass AppService {\n  private items: { id: number; name: string }[] = [];\n  constructor() {\n    this.items.push({ id: 1, name: 'Item 1' });\n  }\n\n  getAllItems(): { id: number; name: string }[] {\n    return this.items;\n  }\n\n  getItemById(id: number): { id: number; name: string } | undefined {\n    return this.items.find(item => item.id === id);\n  }\n\n  createItem(name: string): { id: number; name: string } {\n    const newItem = { id: Date.now(), name };\n    this.items.push(newItem);\n    return newItem;\n  }\n}\n\n// 2. Define a Controller\n@Controller('items')\nclass AppController {\n  constructor(private readonly appService: AppService) {}\n\n  @Get()\n  getAllItems(): { id: number; name: string }[] {\n    return this.appService.getAllItems();\n  }\n\n  @Get(':id')\n  getItem(@Param('id') id: string): { id: number; name: string } {\n    const foundItem = this.appService.getItemById(parseInt(id, 10));\n    if (!foundItem) {\n      throw new HttpException('Item not found', HttpStatus.NOT_FOUND);\n    }\n    return foundItem;\n  }\n\n  @Post()\n  createItem(@Body('name') name: string): { id: number; name: string } {\n    if (!name) {\n      throw new HttpException('Name is required', HttpStatus.BAD_REQUEST);\n    }\n    return this.appService.createItem(name);\n  }\n}\n\n// 3. Define the Root Module\n@Module({\n  imports: [],\n  controllers: [AppController],\n  providers: [AppService],\n})\nclass AppModule {}\n\n// 4. Bootstrap the Application\nasync function bootstrap() {\n  const app = await NestFactory.create(AppModule);\n  await app.listen(process.env.PORT ?? 3000);\n  console.log(`Application is running on: http://localhost:${process.env.PORT ?? 3000}`);\n}\n\nbootstrap();","lang":"typescript","description":"This quickstart demonstrates a basic NestJS application with a service, controller, and module, showcasing dependency injection and routing. It creates a simple CRUD API for 'items', listening on port 3000."},"warnings":[{"fix":"Upgrade your Node.js environment to version 16 or newer. Update your package.json `engines` field.","message":"NestJS v10 dropped support for Node.js v12. Applications must run on Node.js v16 or higher.","severity":"breaking","affected_versions":">=10.0.0"},{"fix":"Install `@nestjs/cache-manager` and `cache-manager` packages (`npm i @nestjs/cache-manager cache-manager`). Update imports from `@nestjs/common` to `@nestjs/cache-manager` for `CacheModule`.","message":"The `CacheModule` was moved from `@nestjs/common` to a standalone package `@nestjs/cache-manager` in v10.","severity":"breaking","affected_versions":">=10.0.0"},{"fix":"Ensure your project uses TypeScript v4.8 or higher. Update your `typescript` dependency in `package.json`.","message":"NestJS CLI plugins (for `@nestjs/swagger` and `@nestjs/graphql`) require TypeScript >= v4.8 since v10 due to AST breaking changes.","severity":"breaking","affected_versions":">=10.0.0"},{"fix":"Review Express v5 migration guides, specifically regarding route syntax. Wildcards now require names (e.g., `/*splat` instead of `/*`), and optional segments use braces (`/:file{.:ext}`).","message":"NestJS v11 defaults to Express v5, which includes breaking changes in its path route matching algorithm, particularly for wildcards (`*`) and optional characters (`?`).","severity":"breaking","affected_versions":">=11.0.0"},{"fix":"Use `forwardRef()` for modules or services when a circular dependency is unavoidable. Refactor your application design to reduce coupling if possible.","message":"Circular dependencies can occur in NestJS modules and providers, leading to cryptic error messages and runtime issues.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Always decorate services with `@Injectable()` and ensure they are listed in the `providers` array of their respective module. If the service is used in another module, it must also be in the `exports` array of its defining module and the `imports` array of the consuming module.","message":"Forgetting to apply `@Injectable()` to a provider (service) or including it in a module's `providers` array will prevent dependency injection from working, resulting in `Can't resolve dependencies` errors.","severity":"gotcha","affected_versions":">=4.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"1. Ensure the dependency class is decorated with `@Injectable()`. 2. Add the dependency to the `providers` array of the current module. 3. If the dependency is from another module, ensure that module exports the dependency, and the current module imports that module.","cause":"A provider (service) or controller attempts to inject a dependency that is not available in its module's `providers` array, or the dependency's module has not been imported or exported correctly.","error":"Nest can't resolve dependencies of the [Service Name] (?). Please make sure that the argument [Dependency Name] at index [index] is available in the [Module Name] context."},{"fix":"Use `forwardRef(() => MyModule)` when importing modules or `forwardRef(() => MyService)` when injecting providers in a constructor to break the circular dependency. Consider refactoring to reduce tight coupling.","cause":"Two or more modules or providers have a direct or indirect dependency on each other, forming a cycle.","error":"Error: Nest circular dependency detected"},{"fix":"Ensure your `tsconfig.json` `module` option is set to `CommonJS` for Node.js environments. For ESM-only dependencies, you might need to configure your build tool (e.g., Webpack, SWC) to transpile them correctly, or configure Node.js to run in ESM mode (which requires specific NestJS setup). NestJS 10+ targets ES2021 by default.","cause":"Although NestJS code uses ESM `import` syntax, the transpiled output typically runs as CommonJS. This error can occur if a third-party library is truly ESM-only and NestJS's build configuration doesn't handle it, or if you're trying to use `require()` for a module that NestJS compiles as ESM.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Verify that `AppModule` is correctly imported in `main.ts` (e.g., `import { AppModule } from './app.module';`) and that `AppModule` class is `exported` from its file.","cause":"Often occurs when `NestFactory.create` is called with an `AppModule` that is `undefined`, usually due to an incorrect import path or a forgotten `export` statement for `AppModule`.","error":"TypeError: Cannot read properties of undefined (reading 'create')"}],"ecosystem":"npm"}