Heroku local doesn't read my .env file - heroku

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.

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.

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.

Related

Laravel on AWS: APP_DEBUG not respected

I initially deployed my Laravel app with APP_DEBUG set to true, but now we're in production I don't want it to whoops! every time there's an error.
I've changed the contents of our EB .config file so that APP_DEBUG: false and I can see the change in Elastic Beanstalk's environment properties:
But Laravel itself is still dumping everything to the screen when there's an error.
I've tried ssh-ing into our server and running php artisan config:clear to see if it was that, but it still didn't work.
I don't understand why Laravel isn't respecting the updated configuration on deployment. Can anyone explain the logic here?
Update: I updated the security settings on the instance and noticed that it was giving our custom error screen. Can anyone explain what happened? Was restarting the server after running php artisan config:clear what did it?
I had issues like this before where I changed something in the console's environment properties which did not correspont with what I got using tinker. You can find the env file on your instance here:
/opt/elasticbeanstalk/deployment/env
if you open the file you see that variables set in .env are not quoted so if you have a password with for example a hashtag or a name with spaces it can result in unintended problems.
I would suggest to stop using environment variables via .yaml config files and start deploying your .env to the elastic beanstalk S3 bucket and fetching it on deployment. This will result in you having more control over the content of the file.
Example of this can be found here:
https://github.com/rennokki/laravel-aws-eb/blob/master/.ebextensions/00_copy_env_file.config

How to add .env file or otherwise set environment variables in a Heroku app?

I've tried many different solutions on the web for this problem, but all have been unsuccessful.
Here's the problem: My app needs to know whether it is being run on Heroku (production mode) or locally (development mode). For this purpose, we want to use environment variables. I've understood that environment variables on Heroku can be set in a .env file. So my attempt was to run heroku run bash -a <app-name> and then to install vim by doing this:
mkdir ~/vim
cd ~/vim
# Staically linked vim version compiled from https://github.com/ericpruitt/static-vim
# Compiled on Jul 20 2017
curl 'https://s3.amazonaws.com/bengoa/vim-static.tar.gz' | tar -xz
export VIMRUNTIME="$HOME/vim/runtime"
export PATH="$HOME/vim:$PATH"
cd -
Apart from crashing repeatedly, vim didn't work anymore when I logged in and out of the shell:
~ $ vim // in the heroku shell
vim: error while loading shared libraries: libXt.so.6: cannot open shared object file: No such file or directory
I also tried heroku plugins:install heroku-vim but running heroku vim after that only resulted in a long delay followed by the normal heroku shell opening, no vim.
I don't really care if I get vim to work. I just want to be able to write in a file named .env on Heroku so I can set environment variables in it.
How can I achieve this?
There is no need for an .env file on Heroku. In fact, such a file won't work very well since
Heroku gets all of its files from your Git repository,
has an ephemeral filesystem, meaning that changes to files like .env will be quickly lost, and
the .env file won't be available on other dynos if you scale your app
As such, creating an .env file on Heroku isn't a good approach.
Instead, you can use its built-in support for environment variables, using heroku config:set <var> <value> or its web UI. Either way, you'll get a regular environment variable.
It is fairly simple.
Just as you added them in your .env file, do the same with heroku's command line and you will see heroku restart and you are all set to fly again.
Just use the command :
(heroku config:set VARIABLE=this_is_the_value)
Remember to use the underscores in the value as spaces are not allowed not inverted quotes (" ")to turn it into a single string is permissible.

Heroku Local [WARN] No ENV file found

When I run heroku local
my console shows me:
[WARN] No ENV file found
How can I fix this?
Add an .env file. This files contains the local VARS that are different in your local setting from the heroku environment. However, if everything runs fine, you can just ignore the warning, or do touch .env.
You can also differentiate by creating a Procfile.local file and run heroku local -f Procfile.local, to force using a local start script.
I just read: https://devcenter.heroku.com/articles/heroku-local. That explains it all.
For a working example: http://github.com/halcwb/GenUnitApp.git. When you have the heroku toolbelt installed you can heroku local -f Procfile.bash on mac or linux, or heroku -f Procfile.local for windows, and go to http://localhost:3000, to check the result.

Heroku using Meteor settings.json On Launch

I've configured a heroku environment variable by using:
heroku config:set METEOR_SETTINGS="$(cat settings.json)"
However, the app fails to launch, and heroku's logs state:
Error: METEOR_SETTINGS are not valid JSON: $(cat settings.json)
Thinking it's not finding my settings.json file. This seems to be the approach I've seen others take. I've tried looking for the root of my app by using
heroku run bash
And changing directories looking for the settings file, and I cannot find it.
It sits in the root directory of my local repository as settings.json
I'm using the following buildpack: https://github.com/jordansissel/heroku-buildpack-meteor.git

Heroku is switching my play framework 2 config file

I have a Play! application which is on Heroku.
My config file is different between my local application and the same on Heroku. Especially for the URL of my MongoDB base.
On localhost my base address is 127.0.0.1 and on heroku it's on MongoHQ. So when I push my application to Heroku I modify my config file.
But some times, like this morning Heroku change the config file. I pushed my application correctly configured on Heroku this morning and everything worked until now.
When I watch the logs I see that Heroku changed my config and try to connect to my local MongoDB base.
Is someone knowing what ? I hope I'm clear :)
Thanks everybody !
If there are differences in your application in different environments (e.g. local vs production), you should be using assigning the values with environment variables. For Play apps, you can use environment variables in your application.conf file, like this:
`mongo.url=${MONGO_URL}`
Then, on Heroku you can set the environment variables with config vars, like this (note, this may already be assigned for you by the add-on provider):
$ heroku config:add MONGO_URL=...
Locally, you can use Foreman to run your application with the environment variables stored in an .env file in your project root.

Resources