Node.js Guide
How to Deploy a Node.js App for Free
Get your Node.js API or app onto the internet in under 20 minutes. Compare three free hosts and deploy step by step.
Quick answer: Use Render for the simplest deploy, Railway for a $5/month credit on any resource, or Fly.io for global distribution. All three auto-deploy from GitHub.
⚡ Host Comparison
| Host | Free Tier | Sleeps? | Best For |
|---|---|---|---|
| Render | 750 hrs/mo web service | Yes (15 min idle) | Simple deploys, beginners |
| Railway | $5 credit/mo | No | APIs + databases together |
| Fly.io | 3 shared VMs + 160GB | No | Global apps, more control |
| Vercel | Serverless functions | N/A | Next.js, static + API routes |
Prepare Your Project
Before deploying, make sure your package.json has:
{
"name": "my-api",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
},
"engines": {
"node": ">=20.0.0"
}
}
- start script — the host runs this to start your app
- engines.node — tells the host which Node version to use
- PORT env variable — use
process.env.PORT, not hardcoded
Use process.env.PORT
Every host assigns a port. Your app must listen on it:
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server on port ${PORT}`);
});
Common bug: Hardcoding app.listen(3000) causes deploy failures. Always read from process.env.PORT.
Push to GitHub
git init
git add .
git commit -m "Ready to deploy"
git branch -M main
git remote add origin https://github.com/USERNAME/REPO.git
git push -u origin main
Make sure .env and node_modules are in .gitignore.
Deploy on Render (Recommended)
- Go to render.com and sign up with GitHub
- Click New → Web Service
- Connect your GitHub repo
- Render auto-detects Node.js — keep default settings
- Add environment variables (see Step 5)
- Click Deploy Web Service
First deploy takes 2–5 minutes. Subsequent deploys are faster.
Add Environment Variables
In your host's dashboard, add every variable from your .env:
DATABASE_URL=postgres://...
JWT_SECRET=your-production-secret
NODE_ENV=production
OPENAI_API_KEY=sk-...
💡 Tip: Use different JWT secrets and API keys for dev and production. Never reuse credentials between environments.
Add a Database (Render / Railway)
Both Render and Railway offer free PostgreSQL:
- Render: New → PostgreSQL → Free tier
- Copy the Internal Database URL
- Add it as
DATABASE_URLto your web service - Redeploy — your app connects automatically
The internal URL only works from services inside the same host — never expose the external URL publicly.
Deploy Updates
Push to your main branch:
git add .
git commit -m "Fix bug"
git push
The host auto-deploys within 1–2 minutes. Pull requests get preview URLs on Render and Railway.
🎯 Production Checklist
- ✅ Environment variables set in the host dashboard
- ✅ PORT read from
process.env.PORT - ✅ Health endpoint at
GET /healthreturning 200 - ✅ Error handling — never crash on unhandled errors
- ✅ CORS configured for your frontend domain only
- ✅ Logging — console.log is fine, structured logs are better
- ✅ Database connection pooling configured
- ✅ Graceful shutdown — close pool on SIGTERM
- ✅ Custom domain linked (if you have one)
- ✅ HTTPS enforced — all three hosts do this by default
❓ Frequently Asked Questions
Best free host for Node.js?
;Render for simplicity, Railway for APIs + databases together, Fly.io for global distribution.
Do free hosts sleep?
Render sleeps after 15 minutes on the free tier (30–60s cold start). Railway and Fly.io don't sleep on free tiers.
Can I deploy a database with my app?
Yes. Render and Railway both offer free PostgreSQL. Connect via DATABASE_URL.
How do I deploy updates?
Push to main on GitHub. Auto-deploy triggers automatically.
Is the free tier enough for real apps?
For personal projects, yes. For production with real users, you'll want a paid plan after you outgrow the limits.