iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Deploy & PM2

Deploying Node in 2026 - Fly, Render, Railway, Vercel, AWS Fargate. Each has a sweet spot.

Deploy targets

EXAMPLE
# 1. Fly.io - region-aware, container-based, global anycast
# fly.toml (after fly launch)
app = 'myapp'
primary_region = 'syd'

[build]
dockerfile = 'Dockerfile'

[http_service]
internal_port = 3000
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 1

[[vm]]
size = 'shared-cpu-1x'
memory = '256mb'

# Deploy
# fly deploy

# Secrets
# fly secrets set DATABASE_URL=postgres://...


# 2. Render - simplest 'just deploy this Git repo'
# render.yaml
services:
  - type: web
    name: api
    runtime: node
    plan: starter
    buildCommand: npm ci && npm run build
    startCommand: node dist/server.js
    envVars:
      - key: NODE_ENV
        value: production
      - key: DATABASE_URL
        fromDatabase: { name: prod-db, property: connectionString }

databases:
  - name: prod-db
    plan: starter


# 3. Railway - similar to Render; PRs get preview environments


# 4. Vercel - best for Next/Nuxt/SvelteKit; serverless functions for APIs
# vercel.json
{
  'rewrites': [{ 'source': '/api/(.*)', 'destination': '/api/$1' }],
  'functions': {
    'api/**/*.ts': { 'runtime': 'nodejs20.x', 'maxDuration': 10 }
  }
}


# 5. AWS Fargate - container, no servers to manage
# Define a task definition + ECS service via CDK or Terraform
# CodeDeploy or ECS rolling deployments for safe updates


# Across targets, always:
# - Use multi-stage Docker build (smaller image, faster cold start)
# - Inject secrets via the platform's secret store, not env vars in plaintext
# - Add a /healthz endpoint and configure platform health checks
# - Log JSON to stdout; let the platform aggregate
# - Set NODE_OPTIONS='--enable-source-maps'
# - Pin Node major version - never use :latest

# Graceful shutdown is non-optional
process.on('SIGTERM', async () => {
  console.log('shutting down');
  await server.close();
  await db.end();
  process.exit(0);
});

Why it matters

For new side projects, Fly or Render is the fastest path. For production at scale on AWS, Fargate or App Runner gives you containers without K8s. Vercel is unbeatable for SSR frontends. Always graceful shutdown, always health endpoints, always pinned Node.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# PM2 keeps Node running and restarts on crash.
npm i -g pm2
pm2 start src/index.js --name api
pm2 save
Try it Yourself »

Discussion

Loading…