Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress errors of missing .env file for production #58

Closed
jmike opened this issue Mar 17, 2015 · 10 comments
Closed

Suppress errors of missing .env file for production #58

jmike opened this issue Mar 17, 2015 · 10 comments

Comments

@jmike
Copy link

jmike commented Mar 17, 2015

Production environments normally don't have a .env file. In these cases require('dotenv').load() prints the following error to console:

{ [Error: ENOENT, no such file or directory '.env'] errno: -2, code: 'ENOENT', path: '.env', syscall: 'open' }

Shouldn't these errors be suppressed or at least change into something less frightening?

@maxbeatty
Copy link
Contributor

Let's put aside what's normal for production and what's not.

We do try/catch the error and politely console.log it as you've described because it's important to let someone know why dotenv may not be working. If we remove the log line, people are left to figure it out themselves (dig into source, add their own logs, etc.). We could add something like debug but then dotenv would no longer be dependency-free.

What's a solution that satisfies all of these concerns?

@jmike
Copy link
Author

jmike commented Mar 17, 2015

Many thanks for the swift reply. I can only think of 2 possible solutions.

  1. Leave the code structure as-is; wrap the ENOENT error into something friendlier, e.g. ".env file not found". I would also suggest changing console.error to console.log, but that is debatable.
  2. Add a "verbose" or "debug" option to the #load method, set to true by default. That would give us the option to suppress errors by doing require('dotenv').load({verbose: false});

Hope that makes sense,

@jmike
Copy link
Author

jmike commented Mar 31, 2015

Would a pull request help to get this going?

@jcblw
Copy link
Collaborator

jcblw commented Mar 31, 2015

@jmike those always seem to push things along faster 😄

@Neamar
Copy link

Neamar commented Mar 31, 2015

Since i have the same problem, i can volunteer for the PR if needed

@maxbeatty
Copy link
Contributor

Took a stab at this in #62

@jcblw
Copy link
Collaborator

jcblw commented Apr 1, 2015

fixed with #62 release v.1.1.0

now you can silence dotenv

require('dotenv').config({silent: true})

@jcblw jcblw closed this as completed Apr 1, 2015
@kachkaev
Copy link

kachkaev commented Dec 5, 2016

Hey guys,

It's great that there is a way to pass {silent: true} in the code to suppress an error. I'm wondering if something like that is also possible for babel users. Here is how I start my server:

#dev
nodemon src/ --exec \"node --require dotenv/config --require babel-register\"

#prod (not recommended; build instead and then run without babel)
babel-node --require dotenv/config --require babel-register src/

The reason for not using equire('dotenv').config({silent: true}) in src/index.js is that cannot be placed before import x from 'x', which may rely on process.env.*.

Any ideas?

@calebboyd
Copy link

@kachkaev I think you can use dotenv_config_silent=true in node argv.

@kachkaev
Copy link

@calebboyd thanks for the hint, I did not know this trick existed. Looks like in v3.0.0 this is no longer the case though. Errors are not shown by default unless you pass a verbose option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants