5

I tried the heroku node sample at: https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

And used Postgres as the db.

All worked.

Now I wanted to run it local, so I used the sample from: https://devcenter.heroku.com/articles/heroku-local

To get the DATABASE_URL to my .env file - but I couldn't get it to work even though when I ran Heroku local it displayed:

[OKAY] Loaded ENV .env File as KEY=VALUE Format

The database still did not connect and when I added:

 console.log(process.env.DATABASE_URL);

It wrote undefined to the console.

Noam
  • 4,472
  • 5
  • 30
  • 47
  • Created a short blog post on how to solve this issue after encountering it as well: https://www.jtrocinski.com/posts/Heroku-Running_a_Python_App_Locally.html – Jacek Trociński Nov 13 '20 at 06:49

2 Answers2

8

It turned out the for some reason my .env file was not in the correct unicode encoding.

I suspect that the command:

heroku config:get DATABASE_URL -s  >.env

Creates an invalid .env file on my windows machine. It creates a file using UTF-16 LE encoding.

Once I changed it to UTF-8 it all worked.

You can do that in VSCODE by clicking on the encoding in the status bar. enter image description here

Noam
  • 4,472
  • 5
  • 30
  • 47
  • I was pretty excited when I saw your question and answer as I have the same issue with a React application. Sadly, my issue appears different as the file is UTF-8. My .env file works properly for the Heroku Example project, but not my own. @noam, any ideas? – MobileVet Oct 30 '18 at 21:47
  • @MobileVet Rename the .env file - and create your own .env file and copy the text from the one created by heroku to the one you've created. Also - in reality I don't use Heroku to serve locally, and I use an npm package to read the .env file called "dotenv" – Noam Oct 31 '18 at 11:08
  • Excellent find. – Karl Apr 19 '19 at 07:58
  • This solved my problem as well. I find it best to use Git Bash as the default terminal in VS Code to avoid these sort of errors. – Jacek Trociński Nov 12 '20 at 07:16
0

Responding to MobileVet's comment:

If it is a create-react-app, the env var MUST start w/ REACT_APP_. That solved it for me... https://create-react-app.dev/docs/adding-custom-environment-variables/

And don't forget to restart.