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
- Ensure your app listens on
process.env.PORT. - Define a
startscript in package.json. - Containerize with Docker by writing a Dockerfile that copies your code and runs
npm start. - Build and push the image to a registry.
- 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
SIGTERMgracefully leads to dropped connections during rolling updates.