Skip to content

Deploying NestJS on Heroku (The “Push a Button and Pray” Method)

You’ve tamed Vercel, AWS, DigitalOcean, Docker… now it’s time for Heroku. Heroku is like that friend who magically fixes your computer over Zoom.

“You push code, Heroku runs it. That’s it.”

🧐 Why Heroku?

  • Beginner-friendly: No SSH, no Nginx, no PM2 headache.
  • Magic dynos: Your app scales (sometimes magically)
  • Git + Deploy: Push to Git, get a URL
  • Free tier: Perfect for experiments, tests, and showing off

Downsides: free tier sleeps if idle… so your app might take a coffee break. ☕

🗂️ Folder Structure

nest-heroku/
│── src/
│   └── main.ts
│── dist/
│   └── main.js
│── package.json
│── tsconfig.json
│── Procfile
│── .gitignore

⚡ Step 1: Prepare NestJS App

main.ts

Make sure your NestJS app listens on process.env.PORT:

ts
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const port = process.env.PORT || 3000;
  await app.listen(port);
  console.log(`NestJS is running on port ${port}`);
}
bootstrap();

package.json scripts

json
"scripts": {
  "start": "node dist/main.js",
  "start:dev": "nest start --watch",
  "build": "nest build"
}

Heroku will use npm start automatically.

⚡ Step 2: Create a Procfile

Heroku uses this to know how to run your app:

web: npm run start

Simple, elegant, magical. ✨

⚡ Step 3: Git + Heroku CLI

bash
# login to Heroku
heroku login

# create git repo if not already
git init
git add .
git commit -m "Initial commit"

# create Heroku app
heroku create nest-heroku-app

⚡ Step 4: Deploy

bash
# push your code
git push heroku main

Heroku will automatically:

  1. Install dependencies
  2. Build your app (npm run build)
  3. Start the dyno using Procfile

⚡ Step 5: Open Your App

bash
heroku open

Your NestJS app is live on something like:

https://nest-heroku-app.herokuapp.com

You can brag: “I deployed to Heroku without touching Linux once.” 😎

🎯 TL;DR Cheat Sheet

StepCommand / Notes
Prepare appprocess.env.PORT in main.ts
Procfileweb: npm run start
Git init & commitgit init && git add . && git commit -m "Deploy"
Create Heroku appheroku create <app-name>
Deploygit push heroku main
Openheroku open

✅ Pro Tips

  • Use .env + heroku config:set for secrets:
bash
heroku config:set DATABASE_URL=your_db_url SECRET_KEY=shhh
  • Free dynos sleep → add a health ping or wake-up cron if needed
  • Logs: heroku logs --tail

🏁 Summary

  • Heroku = magical, beginner-friendly, one-button deployment
  • Works with dist folder or full source
  • No SSH, no PM2, no Nginx, no crying
  • Perfect for demo apps, small projects, or impressing non-tech friends 😎

Built by noobs, for noobs, with love 💻❤️