Dotenv: Suppress errors of missing .env file for production

Created on 17 Mar 2015  路  10Comments  路  Source: motdotla/dotenv

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?

Most helpful comment

fixed with #62 release v.1.1.0

now you can silence dotenv

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

All 10 comments

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?

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,

Would a pull request help to get this going?

@jmike those always seem to push things along faster :smile:

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

Took a stab at this in #62

fixed with #62 release v.1.1.0

now you can silence dotenv

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

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?

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

@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.

Was this page helpful?
0 / 5 - 0 ratings