-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
env variables not accessible in vite.config.js #1930
Comments
There's a chicken-egg problem here: Vite expects to resolve So if we resolve |
This also applies to postcss file, on v1.x I was able to use variables from .env files, but seems like from v2.x those are not being injected until a later point. Let me know if you need a repository with reproduction 👍 |
we use a simple trick to do this in our project
|
For my use case I wanted to set the @pionxzh I may be wrong but should that be simply:
The type definitions for defineConfig show that it accepts either the config object or a function that returns the config object:
|
@jonjanisch |
found another trick in an older issue (#1096 (comment)), that is also nice and seems to cover import { loadEnv } from 'vite'
export default ({ mode }) => {
Object.assign(process.env, loadEnv(mode, process.cwd()))
...
} |
Thanks for the code of @jmheretik. I'm using this in postcss.config.js.
|
I think this behavior could be documented on https://vitejs.dev/guide/env-and-mode.html, shall I craft a PR? |
if the const envDir = path.resolve(__dirname, "environment");
// see: https://github.com/vitejs/vite/issues/1930#issuecomment-783747858
const loadEnvVariables = (mode: string): void => {
Object.assign(process.env, loadEnv(mode, envDir, ""));
console.debug("process.env contains: \n" + JSON.stringify(process.env, undefined, 2));
};
// https://vitejs.dev/config/
export default defineConfig((init) => {
loadEnvVariables(init.mode);
return {
..... Compared to the other answers, I had to provide an empty string to the 3rd parameter of |
This did not work for me: But this did: |
Proxying based on .env.development does not really work. There are some workarounds: - <https://stackoverflow.com/questions/66389043/how-can-i-use-vite-env-variables-in-vite-config-js> - <vitejs/vite#1930> Not sure if it is worth it ...
This work for me: export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd())
const processEnvValues = {
'process.env': Object.entries(env).reduce(
(prev, [key, val]) => {
return {
...prev,
[key]: val,
}
},
{},
)
}
return {
plugins: [react()],
define: processEnvValues
}
} |
Proxying based on .env.development does not really work. There are some workarounds: - <https://stackoverflow.com/questions/66389043/how-can-i-use-vite-env-variables-in-vite-config-js> - <vitejs/vite#1930> Not sure if it is worth it ...
Another workaround: Just use dotenv and then put the following at the top of your vite config/sveltekit config: import 'dotenv/config'; |
Describe the bug
Variables from
.env*
files are accessible only during development/build but not already invite.config.js
.Im not sure if this behaviour was omitted on purpose because it doesn't align with vite's design or it was simply forgotten, but when I was using
vue-cli
I got used to having the env variables available already in vue.config.js. I find it super convenient to be able to setup the vue/vite configuration parameters based on the values provided by the.env*
files (instead of checking themode
and conditionally setting the parameters to the same values which are already in my.env*
files.My current workaround is to use
dotenv-flow
package to imitate the same behaviour as in vue cli and put this on top of my vite.config.js:require('dotenv-flow').config()
.Reproduction
.env
with some variable: e.g.VITE_BASE_URL=/example/
console.log(process.env.VITE_BASE_URL)
orconsole.log(import.meta.env.VITE_BASE_URL)
somewhere in yourvite.config.js
undefined
System Info
vite
version: 2.0.0-beta.65The text was updated successfully, but these errors were encountered: