Skip to content

NestJS Deployment

Deploying Nest means compiling TypeScript, running the Node process against production config, and operating health checks. This lesson covers a practical production path.

Production Build

Run nest build (or npm run build) to emit JavaScript into dist/. Start with node dist/main.js (or npm run start:prod). Use environment variables for secrets and ports.

npm run build
NODE_ENV=production node dist/main.js

Do not run start:dev in production—watch mode and ts-node are for local development.

Dockerfile Sketch

FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
CMD ["node", "dist/main.js"]
  • Use multi-stage Docker builds to keep runtime images lean.
  • Listen on 0.0.0.0 inside containers.
  • Add a /health endpoint for orchestrator probes.
  • Enable shutdown hooks for graceful SIGTERM handling.

Deployment Cheatsheet

Checklist for shipping Nest.

Item Action
Build nest build -> dist/
Start node dist/main.js
Config Platform env vars / secrets
Health GET /health liveness/readiness
Logs Structured JSON logs to stdout
Scale Stateless instances behind a load balancer

Stay Stateless

Store sessions and uploads in Redis/S3, not local disk, so horizontal scaling works. JWT or shared session stores fit multi-instance apps.

Run Migrations on Deploy

Apply Prisma/TypeORM migrations as a release step before shifting traffic. Never rely on synchronize: true in production.

Common Mistakes

  • Deploying with development logger settings and verbose debug noise.
  • Baking secrets into Docker images.
  • Forgetting database migrations in the release pipeline.
  • Binding only to localhost inside containers.

Key Takeaways

  • Production runs compiled dist output, not watch mode.
  • Containers should be lean, configurable via env, and healthy-probed.
  • Keep apps stateful-free at the instance layer.
  • Migrations are part of deployment, not an afterthought.

Pro Tip

Make /health cheap and dependency-aware: liveness can be process-up, while readiness can verify DB connectivity before receiving traffic.