Noam
Noam

Reputation: 5286

Heroku local doesn't read my .env file

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.

Upvotes: 5

Views: 2298

Answers (2)

user3443993
user3443993

Reputation: 1

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.

Upvotes: 0

Noam
Noam

Reputation: 5286

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

Upvotes: 8

Related Questions