DEV Community

Cover image for How to upload a Nodejs app to Heroku
Ifeanyi Chima
Ifeanyi Chima

Posted on • Edited on

How to upload a Nodejs app to Heroku

If you have a Nodejs server that you want to upload to heroku, here is a guide you can follow. I hope this helps a beginner out there.

BMC coffee

Package.json

Firstly, we need to add the following to our package.json.

{
  "name": "session",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "engines": { // ==> heroku engine
    "node": "16.15.0"
  },
  "scripts": {
    "start": "node ./src/server.js" // ===> start command
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
Enter fullscreen mode Exit fullscreen mode

1. Heroku CLI
I hope you already have the heroku cli installed on your local machine.

2. Heroku Login
Login to your heroku account using terminal.

 heroku login
Enter fullscreen mode Exit fullscreen mode

3. Create Heroku app
Using the terminal, create a heroku app;

 heroku create <app-name>
Enter fullscreen mode Exit fullscreen mode

4. Add Heroku app
You need to set your online (cloud) Heroku app as a remote git repository for your nodejs server.

heroku git:remote -a <app-name>
Enter fullscreen mode Exit fullscreen mode

5. Environmental variables

If your app has .env file, then you would add that in Heroku manually, Find the section named Config Vars and click on Reveal Config Vars. Here you will add the same key and value pairs from the .env file you used locally. You can see mine below with the values blurred out.

Image description

6. Push
Now, we are ready to push our nodejs server to Heroku

git push heroku main
Enter fullscreen mode Exit fullscreen mode

Buy Me A Coffee

Thank you, please follow me

HTML GitHub

Top comments (0)