How to deploy a Node.js application

· Category: Node.js

Short answer

Deploy Node.js apps using platforms like Heroku, AWS, Vercel, or Docker containers orchestrated with Kubernetes.

Steps

  1. Ensure your app listens on process.env.PORT.
  2. Define a start script in package.json.
  3. Containerize with Docker by writing a Dockerfile that copies your code and runs npm start.
  4. Build and push the image to a registry.
  5. Deploy to a platform or cloud provider and configure environment variables.

Tips

  • Use a process manager like PM2 inside containers for graceful restarts and logging.
  • Health check endpoints help orchestrators determine if your app is ready to receive traffic.

Common issues

  • Hardcoding the port causes failures on platforms that assign dynamic ports.
  • Not handling SIGTERM gracefully leads to dropped connections during rolling updates.