I've tried all of the solutions out there but none seem to work for me. I just want to store some values in a .env
file within my Vue app but simply trying to log process.env
returns an empty object from within the component.
My .env
file
VUE_APP_URL={api url} VUE_APP_TOKEN={token}
My plan was to set these environment variables to data properties but it always returns undefined
. If I do console.log(process.env.NODE_ENV)
from webpack.config.js it will show that I'm in development but if I tried doing the same from within the component like
mounted() { this.$nextTick(() => { console.log(process.env.VUE_APP_URL); }) }
It just returns undefined
.
A few tips for people who land here:
.env
files are in the project root folder (and not in say src/
)VUE_APP_
if to be statically embedded into the client bundle:
(from JSON config) with =
(dotenv format). Easy to miss.env
files.config/dev.env.js
instead of the .env
files in rootI figured it out - I had to install dotenv-webpack
and initialize it in webpack.config.js which is odd because none of the docs stated that I needed to do so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With