vite: Exposed env vars are not available
Describe the bug
Using an .env file to define environment variables, static values are passed on and available in Vue as expected. Exposing existing env vars to Vue, however, does not work and produces empty values.
I’ve exposed the env var using the syntax from this comment.
STR:
- open reproduction
- start dev server with command
PORT=4000 npm run dev
Expected:
PORT should be available from main.js and output 4000.
Actual:
PORT is an empty string.
Reproduction
https://stackblitz.com/edit/vitejs-vite-bxj6nd?devtoolsheight=33&file=.env
System Info
System:
OS: macOS 11.6.1
CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
Memory: 1.36 GB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.13.2 - ~/.nix-profile/bin/node
npm: 8.1.2 - ~/.nix-profile/bin/npm
Browsers:
Brave Browser: 97.1.34.81
Safari: 15.2
Used Package Manager
npm
Logs
...
vite:config env: {
vite:config VITE_PORT: '',
vite:config BASE_URL: '/',
vite:config MODE: 'development',
vite:config DEV: true,
vite:config PROD: false
vite:config },
...
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 3
- Comments: 16 (5 by maintainers)
This is about explicitly exposed env vars, not all env vars. Only if you write
in a
.envfile, it should be made available in Vite.Just released https://github.com/motdotla/dotenv-expand/blob/master/CHANGELOG.md#900-2022-08-30
Hi, I am experiencing the dotenv variables expanding issue while trying to develop a component for my vue3 app that displays its current version from
package.json..env
AppVersion.vue
Other solution that worked for me (maybe better in my case) was to use vite’s
defineconfig option:vite.config.ts
.env.d.ts
AppVersion.vue
Hope it helps someone.
I am looking forward guys from dotenv-expand to solve the issue: https://github.com/motdotla/dotenv-expand/issues/55
may be https://github.com/motdotla/dotenv-expand/issues/55 mentioned this
In my case it was an improper Docker / Docker Compose configuration. In my
DockerfileI had to make sure to specify the arguments which I want to pass during Docker build time asARGand assign them toENVvariables, something likeand my
docker-compose.ymllooks likeMaybe this helps 🤷🏼♂️
@svenjacobs The workaround that I’m currently using is to declare the env var as part of the script that I run to start vite, e. g.:
That’s what I tried, please look at the reproduction and debug output above.
You’re right, plus issues here are supposed to be framework agnostic. I’ve removed it from the title, thanks!