Skip to content
Back to Deployment

Deploying NestJS Apps

Enterprise Angular-style Node framework with DI, modules, and decorators, deploy as Node server or serverless.

PlatformsAdvanced

Why

NestJS brings enterprise architecture (dependency injection, modules, decorators) to Node.js, making large backends maintainable. Its Angular-inspired structure scales to large teams and complex domains.

When

Use NestJS for large enterprise backends with complex domains, multiple teams, and long-term maintenance. Avoid for small APIs — overhead isn't worth it.

How

1. Build — nest build produces dist/ with compiled JS 2. Node — node dist/main.js, runs Express/Fastify under the hood 3. Docker — multi-stage build, node:20-alpine runtime 4. Serverless — @nestjs/serverless or serverless.yml with Lambda 5. PM2 — pm2 start dist/main.js -i max for cluster 6. Swagger — auto-generated docs at /api if SwaggerModule configured 7. Microservices — @nestjs/microservices for TCP/Redis/RabbitMQ transport

Example

// src/main.ts — NestJS bootstrap import { NestFactory } from "@nestjs/core"; import { AppModule } from "./app.module"; async function bootstrap() { const app = await NestFactory.create(AppModule); app.enableShutdownHooks(); // graceful shutdown await app.listen(process.env.PORT ?? 3000); } bootstrap(); // Dockerfile FROM node:20-alpine AS build WORKDIR /app COPY . . RUN npm ci && npm run build FROM node:20-alpine WORKDIR /app COPY --from=build /app/dist ./dist COPY --from=build /app/node_modules ./node_modules EXPOSE 3000 CMD ["node", "dist/main.js"] // serverless.yml — Lambda deploy functions: api: handler: dist/main.handler events: - http: { path: /{proxy+}, method: ANY }

Common Problems

  • ⚠DI circular dependency — two providers inject each other, use forwardRef() to break cycle
  • ⚠Module not found in production — dist/ missing files, check nest-cli.json assets and webpack
  • ⚠Serverless cold start 5+ seconds — NestJS bootstraps all modules on cold start, use lazy modules

Best Practices

  • ✓Use enableShutdownHooks() — ensures graceful shutdown for SIGTERM in containerized deploys
  • ✓Keep modules small and feature-scoped — global modules create coupling and slow cold starts
  • ✓Set up health checks and graceful shutdown — handle SIGTERM to drain in-flight requests before exit

FAQ