env-cmd icon indicating copy to clipboard operation
env-cmd copied to clipboard

Variables from multiple .env files

Open plashenkov opened this issue 4 years ago • 10 comments

Hey!

Thanks for the awesome lib!

It would be nice if env-cmd could read variables from multiple .env files. There is a common approach to use .env and .env.local, and maybe .env.<environment> (.env.production, .env.development, etc.) schema.

Here are the examples:

  • https://symfony.com/doc/current/configuration.html#overriding-environment-values-via-env-local (Symfony)
  • https://nextjs.org/docs/basic-features/environment-variables#default-environment-variables (Next.js)
  • https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used (Create React App)

It's a common and nice practice since you can use common defaults in an .env file and then override them in a machine-specific .env.local. Usually you commit .env to a git repository and do not commit .env.local (secrets go there).

Currently it looks like it's impossible to use env-cmd in such way. It would be nice if we could by using some argument, for example:

env-cmd --cascade mocha -R spec
# or different argument name of course

plashenkov avatar Jun 22 '20 13:06 plashenkov

I had been thinking about this idea for a while. Probably the next major rewrite will take this approach. Not sure when that will be, but I have been needing to turn my attention back to this project for a while now. Thanks for the feature request 😄

toddbluhm avatar Jul 01 '20 23:07 toddbluhm

We fixed it in a way by using: env-cmd -f .env.staging env-cmd -f .env.staging.local --silent run-scripts build Because of silent it doesn't matter if you have a local file, but it will be read.

antonia-rose avatar Jul 02 '20 06:07 antonia-rose

Is there any progress?

rxliuli avatar May 12 '21 02:05 rxliuli

It seems that the PR of related functions was merged before, why is the newer version deleted? Related PR: https://github.com/toddbluhm/env-cmd/pull/17

rxliuli avatar Aug 03 '21 03:08 rxliuli

Another use for this is to put project specific variables (such as queue or data source names) in the project, but keep things like AWS tokens in a separate file that is not commit'd.

Shame env-cmd doesn't support this, because otherwise it works well

tomchiverton avatar Aug 10 '21 16:08 tomchiverton

Hi. Any plans for this issue? Is this project still maintained or is there a newer alternative?

john-larson avatar Apr 27 '22 20:04 john-larson

I can't pass more than one .env.* files with just env-cmd -f .env.file1 -f .env.file2?

nemanjam avatar Jun 04 '22 12:06 nemanjam

It is necessary to implement this functionality

jangarach avatar May 11 '23 09:05 jangarach

Workaround that seemed to work for me:

env-cmd -f .env1 env-cmd -f .env2 my_command

sagimann avatar Jun 28 '23 12:06 sagimann

you can use dotenvx for this

# .env
HELLO="World"
# .env.local
HELLO="Local"
// index.js
console.log(`Hello ${process.env.HELLO}`)
dotenvx run --env-file=.env.local --env-file=.env -- node index.js

motdotla avatar Feb 12 '24 05:02 motdotla