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

Impossible to set options in some cases #145

Closed
callumlocke opened this issue Jul 13, 2016 · 3 comments
Closed

Impossible to set options in some cases #145

callumlocke opened this issue Jul 13, 2016 · 3 comments

Comments

@callumlocke
Copy link

Personally I think silent: true would be a much saner default. I know this was discussed before in #111 and rejected. I guess I will just have to set the option manually, so I can use dotenv in code that might get built in CI where there's no .env file.

However, there is no obvious way to use dotenv with non-default options in the following scenario:

  1. you're using the ES2015 import statement to load modules, and
  2. you're using a task runner CLI such as gulp which will choke on dotenv_config_silent=true.

Example gulpfile.babel.js:

import 'dotenv/config';
import gulp from 'gulp';
import foo from './foo.js';

// ...

The problem is the first line will fail on CI where you've got no .env. And you can't change your CI build command to gulp build dotenv_config_silent=true because it will error with "Task 'dotenv_config_silent=true' is not in your gulpfile" (btw, why doesn't dotenv follow the convention of prefixing the argument with --?).

You could try doing import dotenv from 'dotenv'; dotenv.config({silent: true}), but that doesn't work either, because ES2015 imports are hoisted and executed before all other code, i.e. the call to .config() won't happen until after other modules are executed, regardless of what order the lines are in. Therefore if another module (e.g. ./foo.js in the above example) expects environment variables to have been set before it runs, it will fail. You can't even work around this by doing require('dotenv').config({silent: false}) at the very top of the file, because imports still get hoisted and executed first.

The only workarounds I can think of are messy/ugly:

  • move all your tasks into another script, and modify your gulpfile to act as a simple entry point, something like require('dotenv').config({silent: false}); require('./real-gulpfile.js');
  • configure your CI to always create an empty .env file before running each build, just to obviate the need for silent:true

A possible solution would be to add config-silent.js, which would be like config.js but with silent:true as a default. Then people could do import 'dotenv/config-silent';.

@maxbeatty
Copy link
Contributor

just so we're on the same page, your primary concern is a clean log with no output from console.error when a .env file is not present (that's all dotenv does). if you're cool with one extra log line on CI, you can skip reading the rest of this and close the issue.

if you're using babel and gulp, I'm willing to bet you're using a newer version of node that supports preloading modules. that allows you to remove imports and requires of dotenv from your source code and is my personal recommendation on how to use dotenv in newer projects. babel and gulp both support preloading (-r). Here's' an example for babel from our examples directory.

The configuration options you've referenced such as dotenv_config_silent are only supported when preloading. I'm open to other ways of configuring preload as long as it doesn't require any external dependencies, is tested, and documented. The current config code is 2 lines.

@callumlocke
Copy link
Author

Oh wow I thought it actually errored the process, didn't realise it just did a console.error. I think my process was quitting unexpectedly and I assumed it was to do with the error printed by dotenv. Must have been something else.

I might PR something to help with cleaner output in this case at some point, but no need to keep this open. Sorry for my confusion :)

@jcblw
Copy link
Collaborator

jcblw commented Jul 13, 2016

thanks @callumlocke for the feedback

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

No branches or pull requests

3 participants