1---2name: nest-best-practices3description: Implement, review, test, or migrate NestJS applications with version-aware patterns. Use for @nestjs/* APIs, modules, validation, transports, testing, or CLI; inspect the installed major before v12 guidance.4---56# NestJS Best Practices78Use the project's source, package manifest, lockfile, module format, HTTP adapter, and test runner as the primary context. Read only the references relevant to the task.910This skill targets NestJS 12. The npm `latest` tag for `@nestjs/core` was verified as `12.0.1` on 2026-08-29. Treat that patch number as a dated observation: re-check the registry and official documentation before recommending or performing an upgrade.1112## Version Gate13141. Inspect `package.json` and the lockfile for `@nestjs/core`, `@nestjs/common`, platform adapters, CLI, and companion packages.152. Preserve the installed major for ordinary feature and bug-fix work. Do not introduce v12-only APIs into a v10/v11 project.163. For a new project or an explicit upgrade, verify the current stable npm tag and official migration guide. Keep all `@nestjs/*` packages on compatible majors.174. For v11 to v12 work, read [NestJS 12 migration](references/migration-v12.md) before editing dependencies or configuration.185. Preserve the project's CommonJS or ESM choice unless the user requests a module-format migration. Nest 12 packages are ESM, but supported CommonJS applications can consume them on compatible Node.js versions.1920## Working Method2122- Inspect existing module boundaries and nearby patterns before generating code.23- Prefer framework primitives and the adapter already in use. Avoid Express-specific response handling in code that should remain adapter-independent.24- Keep controllers thin: translate transport input/output and delegate business rules to providers.25- Use explicit injection tokens for non-class dependencies. Export only providers that other modules actually consume.26- Choose singleton scope by default; request scope has a performance and dependency-graph cost.27- Validate untrusted input at the transport boundary. Use class-based or Standard Schema-based validation consistently with the project; do not layer both without a concrete reason.28- Keep secrets and environment-specific values outside source. Validate configuration during startup.29- Preserve stable error contracts. In Nest 12, prefer `errorCode` for machine decisions and messages for humans.30- Test observable behavior through `@nestjs/testing`; preserve the repository's Jest or Vitest choice.31- After edits, run the narrow relevant tests, type checking, linting, and build commands already defined by the repository.3233## NestJS 12 Decisions3435- **Runtime and CLI:** a Nest 12 application needs Node.js 20.19+ or 22.12+ on the 22.x line. Current schematics/CLI generation has a higher runtime floor; see the migration reference rather than inferring from `@nestjs/core.engines` alone.36- **Validation:** keep `ValidationPipe` for decorator/class DTOs. Use the built-in `StandardSchemaValidationPipe` with `@Body({ schema })`, `@Query({ schema })`, or `@Param(..., { schema })` when the project already uses Zod, Valibot, ArkType, or another Standard Schema library.37- **Serialization:** keep `ClassSerializerInterceptor` for class-transformer models. Use `StandardSchemaSerializerInterceptor` with `@SerializeOptions({ schema })` for schema-driven response shaping.38- **Configuration:** `@nestjs/config` accepts Standard Schema through `validationSchema`; Zod is the current documentation's default example. Joi requires v18+ and nests library-specific settings under `validationOptions.libraryOptions`.39- **CLI/build:** use `nest upgrade --dry-run` before an explicit migration. Rspack is the v12 monorepo default; webpack CLI flags are deprecated. Do not migrate an existing build stack merely to match new-project defaults.40- **Routes:** consider `routeConflictPolicy` and `routeResolutionStrategy: 'specificity'` when route shadowing is a real risk; both are opt-in.41- **GraphQL:** GraphiQL is the default IDE. Use `graphql-ws`; `subscriptions-transport-ws` is no longer supported by the latest GraphQL package.42- **NATS:** v12 uses NATS v3 and `@nats-io/transport-node`; review custom serializers/deserializers.43- **Observability:** `@nestjs/observe` is an optional official hosted integration, not a universal dependency. Read [observability](references/observability.md) only when the task calls for it.4445## Reference Routing4647### Migration, CLI, and Architecture4849| Need | Read |50|---|---|51| Upgrade v11 to v12, Node/ESM/tooling changes | [migration-v12](references/migration-v12.md) |52| CLI commands, generators, builders | [cli-overview](references/cli-overview.md) |53| Monorepos and libraries | [cli-monorepo](references/cli-monorepo.md) |54| Controllers and route resolution | [core-controllers](references/core-controllers.md) |55| Modules and exports | [core-modules](references/core-modules.md) |56| Providers and custom providers | [core-providers](references/core-providers.md) |57| Dependency injection | [core-dependency-injection](references/core-dependency-injection.md) |58| Middleware | [core-middleware](references/core-middleware.md) |5960### Request Lifecycle and Framework Primitives6162| Need | Read |63|---|---|64| Complete request execution order | [best-practices-request-lifecycle](references/best-practices-request-lifecycle.md) |65| Pipes | [fundamentals-pipes](references/fundamentals-pipes.md) |66| Guards and metadata | [fundamentals-guards](references/fundamentals-guards.md) |67| Interceptors | [fundamentals-interceptors](references/fundamentals-interceptors.md) |68| HTTP exceptions and filters | [fundamentals-exception-filters](references/fundamentals-exception-filters.md) |69| Execution context | [fundamentals-execution-context](references/fundamentals-execution-context.md) |70| Custom decorators | [fundamentals-custom-decorators](references/fundamentals-custom-decorators.md) |71| Dynamic modules | [fundamentals-dynamic-modules](references/fundamentals-dynamic-modules.md) |72| Provider scopes | [fundamentals-provider-scopes](references/fundamentals-provider-scopes.md) |73| Lifecycle hooks and shutdown | [fundamentals-lifecycle-events](references/fundamentals-lifecycle-events.md) |74| Lazy loading | [fundamentals-lazy-loading](references/fundamentals-lazy-loading.md) |75| Circular dependencies | [fundamentals-circular-dependency](references/fundamentals-circular-dependency.md) |76| Runtime provider lookup | [fundamentals-module-reference](references/fundamentals-module-reference.md) |77| Unit and e2e testing | [fundamentals-testing](references/fundamentals-testing.md) |7879### HTTP, Data, and Operations8081| Need | Read |82|---|---|83| Class or Standard Schema validation | [techniques-validation](references/techniques-validation.md) |84| Class or Standard Schema serialization | [techniques-serialization](references/techniques-serialization.md) |85| Configuration validation | [techniques-configuration](references/techniques-configuration.md) |86| Databases overview | [techniques-database](references/techniques-database.md) |87| TypeORM | [recipes-typeorm](references/recipes-typeorm.md) |88| Prisma | [recipes-prisma](references/recipes-prisma.md) |89| Mongoose | [recipes-mongoose](references/recipes-mongoose.md) |90| Caching and Keyv | [techniques-caching](references/techniques-caching.md) |91| Logging | [techniques-logging](references/techniques-logging.md) |92| Official Nest observability | [observability](references/observability.md) |93| File uploads | [techniques-file-upload](references/techniques-file-upload.md) |94| API versioning | [techniques-versioning](references/techniques-versioning.md) |95| Queues and BullMQ | [techniques-queues](references/techniques-queues.md) |96| Scheduling | [techniques-task-scheduling](references/techniques-task-scheduling.md) |97| Events | [techniques-events](references/techniques-events.md) |98| Outbound HTTP | [techniques-http-module](references/techniques-http-module.md) |99| Fastify | [techniques-fastify](references/techniques-fastify.md) |100| Sessions and cookies | [techniques-sessions-cookies](references/techniques-sessions-cookies.md) |101| Compression, streaming, and SSE | [techniques-compression-streaming-sse](references/techniques-compression-streaming-sse.md) |102| MVC and static assets | [techniques-mvc-serve-static](references/techniques-mvc-serve-static.md) |103| Raw body and hybrid apps | [faq-raw-body-hybrid](references/faq-raw-body-hybrid.md) |104105### Security and API Contracts106107| Need | Read |108|---|---|109| Authentication | [recipes-authentication](references/recipes-authentication.md) |110| RBAC, claims, and policies | [security-authorization](references/security-authorization.md) |111| CORS, Helmet, and throttling | [security-cors-helmet-rate-limiting](references/security-cors-helmet-rate-limiting.md) |112| Encryption and password hashing | [security-encryption-hashing](references/security-encryption-hashing.md) |113| OpenAPI, CLI plugin, Standard Schema | [openapi-swagger](references/openapi-swagger.md) |114115### GraphQL, WebSockets, and Microservices116117| Need | Read |118|---|---|119| GraphQL setup | [graphql-overview](references/graphql-overview.md) |120| Resolvers and mutations | [graphql-resolvers-mutations](references/graphql-resolvers-mutations.md) |121| Subscriptions | [graphql-subscriptions](references/graphql-subscriptions.md) |122| Scalars, unions, and enums | [graphql-scalars-unions-enums](references/graphql-scalars-unions-enums.md) |123| WebSocket gateways | [websockets-gateways](references/websockets-gateways.md) |124| WebSocket filters, guards, and adapters | [websockets-advanced](references/websockets-advanced.md) |125| Microservices fundamentals | [microservices-overview](references/microservices-overview.md) |126| Redis, Kafka, NATS, and RabbitMQ | [microservices-transports](references/microservices-transports.md) |127| gRPC | [microservices-grpc](references/microservices-grpc.md) |128129### Advanced Recipes130131| Need | Read |132|---|---|133| CQRS | [recipes-cqrs](references/recipes-cqrs.md) |134| Health checks | [recipes-terminus](references/recipes-terminus.md) |135| CRUD generator | [recipes-crud-generator](references/recipes-crud-generator.md) |136| Swagger recipe | [recipes-documentation](references/recipes-documentation.md) |