I have just started using Heroku and I think that I may be facing a potential problem. I have a GitHub repository that contains the code for a Rest API that I have made. However, a config file has not been published as it contains the password to connect to my database (MongoDB Atlas). I want to port my Node.js API to Heroku, but I do not think that just deploying the code from GitHub will work. What should I do? I have Git and the Heroku CLI installed, and use Linux.
Asked
Active
Viewed 270 times
1 Answers
2
Use Environment variables on Heroku. As you rightly mentioned in your question, it is not a good idea to push credentials and other sensitive information in code repositories. So instead of pushing .env file, just create Environment Variables on Heroku. It can be done using Heroku web interface or using Heroku CLI too.
Sample commands on CLI will look like
To get existing config variables
heroku config
To get a specific config variable
heroku config:get GITHUB_USERNAME
To set a config variable
heroku config:set GITHUB_USERNAME=joesmith
To delete a config variable
heroku config:unset GITHUB_USERNAME
To access variable inside your Node.js app,
process.env.GITHUB_USERNAME

Bilal Akbar
- 4,659
- 1
- 18
- 29
-
OK! I will push the code to GitHub, test it out and tell you if it worked! :) – Diogenis Siganos Jul 08 '20 at 10:55
-
I get this error on the CLI: › Error: Missing required flag: › -a, --app APP app to run command against › See more help with --help – Diogenis Siganos Jul 08 '20 at 10:59
-
@Bonfire specify the name of your app on Heroku. At the end of command -a app-name – Bilal Akbar Jul 08 '20 at 11:03
-
Well, the code was not able to find the environment variable in my local machine, so I am porting it to Heroku and I will test it there. It is taking a long time to load, though. Hope it is normal – Diogenis Siganos Jul 08 '20 at 11:26
-
Kindly read about [Environment Variables in Node.js](https://stackoverflow.com/a/34154491/2803788) and [How to deploy Node.js app on Heroku](https://devcenter.heroku.com/articles/getting-started-with-nodejs). And please don't ask new questions in comments section. – Bilal Akbar Jul 08 '20 at 11:38
-
The second one is exactly the guide I am following – Diogenis Siganos Jul 08 '20 at 11:39
-
Works fine! Thanks! – Diogenis Siganos Jul 08 '20 at 17:35