dotenv: Deployment to Heroku fails if dotenv is required in production
Documentation should be instructing users to require dotenv only in development. I just ran through this yesterday with the Sinatra version of dotenv as well and the same as happened in Express. Based on the change below, a simple Express scaffold will or won’t run on Heroku.
if (app.get('env') == 'development'){ require('dotenv').config(); }
About this issue
- Original URL
 - State: closed
 - Created 8 years ago
 - Comments: 15 (5 by maintainers)
 
Commits related to this issue
- modify .env handling https://github.com/motdotla/dotenv/issues/126 — committed to MannyIkomi/graphql-behance-api by MannyIkomi 5 years ago
 
We log an error if anything goes wrong reading the file, parsing it, or assigning to
process.env(src). This seems to scare more people than it helps.This has been covered many times before but it’s quicker just to type it out than find all the other references.
dotenvcan be used safely in any environment (development, staging, qa, production, etc.).envfile to source control. Don’t put any sensitive data in source control.require('dotenv').config({ silent: process.env.NODE_ENV === 'production' })). This will keep your Heroku deploy logs clean (or any other PaaS like AWS Elastic Beanstalk where environment variables are populated for you)@gkatsanos,
dotenvis a good tool to read a.envfile and assign those variables toprocess.env.process.envwill contain the current environment’s env vars, sodotenvdoesn’t have to be used in those cases. What this ends up looking like is that you want to usedotenvin development to load your.envfile but not in production since the environment should have the env vars you need (i.e., inprocess.env).A way you can keep
dotenvoutside your code and still benefit from it is by using this in yourpackage.jsonfor development:"dev": "node -r dotenv/config ."If you don’t want to use
dotenv, you can always replace that line with the following:"dev": "env $(cat .env | grep -v ^# | xargs) node ."Hope this helps!
dotenvcan be used safely in any environment (development, staging, qa, production, etc.)@guilsa, the reason it fails is because
.envdoesn’t exist and thus whendotenvtries to read it there is an error.I still fail to see the usefulness of this module if it requires manual actions and I guess I m not the only one
@guilsa it should work no matter what environment that you use it in, maybe this is something application specific that is breaking when an incorrect env is loaded? Do you have a, shareable, stack trace of the crash that dotenv is creating?
We do have mention inside of our README that the
.envfile should not be committed.as well a deter people from using it in multiple environments
I would say it should run fine if ran in the other environments, and the recommending the usage of
dotenvto be something like…would be kinda weird to put in our README.
Please use GitHub Issues to advance this project instead of venting your own frustrations. We are very open to improving the developer experience through better documentation and other messaging.
i just stumbled on to this thread and am confused with the conflicting statements here.
do i then set env vars manually?
Yeah, like @jcblw said, this is likely because you are committing your
.envto version control @guilsa.Going to go ahead and close this with that assumption. If that is not the case let us know and we’ll dive deeper.