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

Can i place all .env files in a directory? #2506

Closed
Shiroyan opened this issue Sep 13, 2018 · 9 comments
Closed

Can i place all .env files in a directory? #2506

Shiroyan opened this issue Sep 13, 2018 · 9 comments

Comments

@Shiroyan
Copy link

What problem does this feature solve?

I don't like placing all .env files in porject root , it makes my project structure look lengthy, can i make a directory to place them in it ?
image

What does the proposed API look like?

I can config it in vue.config.js

@jtommy
Copy link
Contributor

jtommy commented Dec 5, 2018

@LinusBorg I'm working on it and I thought to add a new option environmentsDir into vue.config.js.
Is it a good solution ?
Because I have just seen that it's possible to change the vue.config.js location using an env variable (process.env.VUE_CLI_SERVICE_CONFIG_PATH) and I need to load environment files after vue.config.js.

@LinusBorg
Copy link
Member

might be a nice idea, yes.

@yyx990803
Copy link
Member

Our overall guideline is - all CLI projects should have consistent file structures as much as possible. The CLI is opinionated by nature. This is why we do not provide options for configuring source directory or public directory names. If we add an option for this we essentially need to add options for renaming every possible config file and directory, and that would

  1. make it more work to maintain all these options
  2. increase possibility of edge cases due to different directory names
  3. make Vue CLI projects all look different from one another.

@chenxeed
Copy link

chenxeed commented Sep 24, 2020

Hello giving second thought of this.

As in my project structure, it's a monorepo that contain backend API and frontend website together.

my-project/
--server/ (node app)
--client/ (vue-cli)

Both may share the same variable, thus it's best if they can refer to the same .env. So my thought is to put the .env file in the root of the project folder:

my-project/
-- server/ (node app)
-- client/ (vue-cli)
-- .env

So, if it's related to the discussion here, can the vue-cli compiler in folder client still able to retrieve the env value from the root directory?

If there's a better suggestion to resolve this, please share ;)

@mihailtd
Copy link

mihailtd commented Oct 1, 2020

I have the exact same issue as @chenxeed
As a workaround I installed node-env-run as a dev dependency, and now my dev script looks like:

"dev": "nodenv --env ../../.env --exec 'vue-cli-service serve'",

Not ideal but works for now.

@c2huc2hu
Copy link

c2huc2hu commented Mar 5, 2021

Another workaround that supports variable expansion:

Add to the beginning of vue.config.js:

const dotenv = require('dotenv')
const dotenvExpand = require('dotenv-expand')
var myEnv = dotenv.config({path: '../.env'})
dotenvExpand(myEnv)

@Lejla94
Copy link

Lejla94 commented Oct 25, 2021

const dotenv = require('dotenv')
const dotenvExpand = require('dotenv-expand')
var myEnv = dotenv.config({path: '../.env'})
dotenvExpand(myEnv)

this works with a single .env for me, but what if i want to put on the same folder all the env files?
it is working with var myEnv = dotenv.config({path: './mypath/.env.production.local'});
i have a .env and a .env.development and a .env.production.local inside ./myPath/*

i tried

var myEnv = dotenv.config({path: '../.env'})
var myEnv = dotenv.config({path: '../.env.development'})
var myEnv = dotenv.config({path: '../.env.prouduction.local'})
dotenvExpand(myEnv, myEnv2, myEnv3)

or

var myEnv = dotenv.config({path: './myPath/'})

i just all env files to be called inside that folder

@c2huc2hu
Copy link

Ideally you shouldn't use multiple dotenv files. At the same tume. https://12factor.net/config

Instead, have multiple separate files.

If you really want to do this though, I suspect this would work:

[myEnv, myEnv2, myEnv3].map(dotenvExpand)

@Lejla94
Copy link

Lejla94 commented Oct 26, 2021

thanks.

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

No branches or pull requests

8 participants