Skip to content

Env variables don't seem to load from a .env file on the new cli #767

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

Closed
alexjoverm opened this issue Feb 2, 2018 · 16 comments
Closed

Env variables don't seem to load from a .env file on the new cli #767

alexjoverm opened this issue Feb 2, 2018 · 16 comments

Comments

@alexjoverm
Copy link

Version

3.0.0-alpha.5

Reproduction link

https://github.com/alexjoverm/vue-cli-env-reproduce

Steps to reproduce

Clone, run npm install and npm run serve on the repo provided.

What is expected?

According to the code of the Service, some env variables should be loaded taken from a path, which are parsed on the loadEnv util.

By adding some logs, it seems that indeed the path it takes when running npm run serve is ${rootDirectory}/.env.development and ${rootDirectory}/.env.development.local. They're indeed loaded.

So, by the console.log(process.env) line in the main.js file, those variables should be printed in the console.

What is actually happening?

They're not listed in the console.

Note: if this is not supposed how is it possible to add env variables?

@yyx990803
Copy link
Member

yyx990803 commented Feb 2, 2018

Only variables that starts with VUE_APP_ are loaded on the client: https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-service/lib/util/resolveClientEnv.js#L1

@e200
Copy link

e200 commented Oct 17, 2018

Why? It makes my API keys very longs.

@irufus
Copy link

irufus commented Feb 5, 2019

Why is this not at the very top part of the documentation?

Especially where you give your example

FOO=bar
VUE_APP_SECRET=secret

FOO should be VUE_APP_FOO

Edited:
It is documented but easily overlooked.

@kispi
Copy link

kispi commented Feb 6, 2019

Why is this not at the very top part of the documentation?

Especially where you give your example

FOO=bar
VUE_APP_SECRET=secret

FOO should be VUE_APP_FOO

Edited:
It is documented but easily overlooked.

Yes I also overlooked

I was yelling on myself 'why it's not working!' like 10 minutes and eventually came here

READ THE DOCUMENTATION
"EACH AND EVERY CHARACTER"

@3zzy
Copy link

3zzy commented Feb 16, 2020

For those who arrived here looking for a solution, but already have your variables prefixed with VUE_APP_, just restart.

@gregfr
Copy link

gregfr commented Feb 29, 2020

just restart.

This should be stressed out in the docs, I spent two hours figuring out why the documented example was not working...

@malandles
Copy link

I understand that the prefix is a good thing to have when your Vue app is coupled with another framework like Laravel, but when the app is standalone, the prefix requirement is a bit exagerated no?

Is there any other reason for the hard requirement? As I can see, we are not the first to overlook the requirement, and the docs are still showing FOO=bar.

@FernandoBasso
Copy link

I noticed that I cannot use it with Ramda.

This code...

console.log(pathOr('what', ['env'], process));
console.log(pathOr('the', ['env', 'VUE_APP_TITLE'], process));
console.log(pathOr('poop 💩', ['env', 'VUE_APP_TITLE'], process));

...produces this output:

image

@MariaIm
Copy link

MariaIm commented Jul 31, 2020

Use touch to create your environment files.
Install touch with npm install touch-cli -g
Create your environment files in root folder with touch .env.production and touch .env.development

@towry
Copy link

towry commented Aug 31, 2020

In development mode, double check that the env file name is correct(not misspelled): ".env.development" or ".env.development.local".

@andyslack
Copy link

I understand that the prefix is a good thing to have when your Vue app is coupled with another framework like Laravel, but when the app is standalone, the prefix requirement is a bit exagerated no?

Is there any other reason for the hard requirement? As I can see, we are not the first to overlook the requirement, and the docs are still showing FOO=bar.

Totally agree with this.

@pushpesh4u
Copy link

To anyone who is still not getting it to work, please make sure your env files (.env, .env.development, .env.development.local, .env.production) have a UTF-8 encoding. My files had a UCS-2 LE BOM encoding and vue was silently ignoring them.

They started working after changing to UTF-8.

@fokosun
Copy link

fokosun commented Dec 13, 2020

still freaking undefined!

@jakedurell
Copy link

This is working for me fine on mac with VUE 2.6.11 in package.json and a .env. When running npm run serve, the .env loads and keys work. This same thing is not working on windows.

@EuniceB
Copy link

EuniceB commented Jan 23, 2021

Also make sure the .env file is in the root of the project, and not inside src

@adeyinkakoya
Copy link

Use touch to create your environment files.
Install touch with npm install touch-cli -g
Create your environment files in root folder with touch .env.production and touch .env.development

The only thing that worked

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