How to Deploy a Node.js App on Heroku: A Quick Start Guide
Deploying a Node.js application to Heroku is straightforward. This tutorial walks you through the essential steps, from setting up your app to going live. You’ll need Git, Node.js installed, and a free Heroku account.
First, ensure your app is ready. Heroku looks for a package.json file to detect Node.js. Your app should also include a start script: add "start": "node app.js" in your package.json. Create a Procfile in the project root with web: npm start — this tells Heroku how to run your app.
1. Install and Login to Heroku CLI
Download the Heroku CLI for your OS. After installation, open a terminal and run:
heroku login— authenticates your account.heroku create your-app-name— creates a new application on Heroku and adds a Git remote.
2. Deploy via Git
Commit your code and push:
git init(if not already a repo)git add .git commit -m "Initial commit"git push heroku main(ormasterdepending on your branch name)
Heroku will automatically detect Node.js, install dependencies from package.json, and start your app.
3. Environment Variables
Store sensitive data like API keys using Heroku config vars. Set them via CLI:
heroku config:set DB_URL=mongodb://...
Access them in your Node.js code via process.env.DB_URL. You can also manage variables in the Heroku Dashboard under Settings → Config Vars.
4. Verify and Scale
Run heroku open to view your live app. To see logs, use heroku logs --tail. If you need more resources, scale your dyno with heroku ps:scale web=1.
That’s it — your Node.js app is now live on Heroku. Remember to keep your Procfile and package.json correct for smooth deploys. Happy coding!