Node.js Deployment

Deployment is the point where a local project becomes a service others depend on. Keep the release path boring and repeatable.

Package release

Production builds should install dependencies deterministically from your lockfile and exclude development-only packages such as test runners and linters. Pin your Node LTS version in package.json (engines.node) or your container base image so runtime behavior matches staging:

# Install exact lockfile versions without devDependencies
npm ci --omit=dev

# Build TypeScript or assets, then start with production env
NODE_ENV=production node dist/app.js
  • Set a clear "start" script in package.json that runs the compiled output.
  • Bundle or copy only the runtime files your container or host needs.
  • Keep build artifacts (dist/) separate from authored source control.

Configuration

Use environment variables for secrets, database URLs, and environment-specific flags—never commit .env files or credentials into Git. Validate all required environment variables when the process boots so misconfigured deployments fail immediately with a clear log message rather than crashing on the first user request.

Cloud checklist

Before routing live traffic to a new Node service, verify startup, observability, and graceful shutdown (handling SIGINT / SIGTERM so in-flight requests finish before the process exits):

  1. Confirm the app starts locally with NODE_ENV=production and the exact production command.
  2. Verify structured JSON logs, a /health endpoint, and dynamic process.env.PORT binding.
  3. Document and test rollback steps (redeploying the previous immutable image or commit) before the first release.

Practice

Write a short deployment runbook for one Node service covering its start command, required environment variables, health-check path, and rollback procedure.