How to Deploy a Node.js App on Heroku: A Step-by-Step Guide
Heroku is a cloud platform as a service (PaaS) that lets you deploy, manage, and scale Node.js apps with minimal effort. You only need a Heroku account, Git, and the Heroku CLI installed. Follow this guide to go from local code to a live URL in minutes.
Before you start, make sure your app has a package.json file with a "start" script (e.g., "start": "node app.js"). Also set the Node.js engine in package.json (optional but recommended) and create a Procfile that declares the process type: web: node app.js.
1. Initialize Git and Create the Heroku App
Run these commands in your project folder:
git initheroku create your-app-name(or omit to get a random name)git add .andgit commit -m "initial commit"
The heroku create command adds a remote named heroku to Git and provisions a new app on Heroku’s servers.
2. Deploy with Git Push
Push your code to the heroku remote:
git push heroku main
Heroku automatically detects the Node.js buildpack, installs dependencies (npm install), and starts the web process from your Procfile. Watch the logs in your terminal to confirm success.
3. Configure Environment Variables
Set any secrets (API keys, passwords) using:
heroku config:set MY_VAR=value
Access them in your app with process.env.MY_VAR. This keeps sensitive data out of your repository.
4. Verify and Scale
Open your app with heroku open to see it live. You can scale dynos (e.g., run two web processes) using heroku ps:scale web=2. Monitor logs with heroku logs --tail for real‑time debugging.
Deploying a Node.js app on Heroku is quick and requires no server management. You can now iterate with git push origin main and then git push heroku main to update your live app. For advanced needs, explore add‑ons like databases or caching.