question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

env variables not accessible in vite.config.js

See original GitHub issue

Describe the bug

Variables from .env* files are accessible only during development/build but not already in vite.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 the mode 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

  1. create .env with some variable: e.g. VITE_BASE_URL=/example/
  2. put console.log(process.env.VITE_BASE_URL) or console.log(import.meta.env.VITE_BASE_URL) somewhere in your vite.config.js
  3. its undefined

System Info

  • vite version: 2.0.0-beta.65
  • Operating System: Windows 10 64bit
  • Node version: v12.20.1
  • Package manager (npm/yarn/pnpm) and version: npm 6.14.10

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:16
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

36reactions
jmheretikcommented, Feb 22, 2021

found another trick in an older issue (https://github.com/vitejs/vite/issues/1096#issuecomment-729077386), that is also nice and seems to cover .env.*.local files too:

import { loadEnv } from 'vite'

export default ({ mode }) => {
  Object.assign(process.env, loadEnv(mode, process.cwd()))
  ...
}
18reactions
pionxzhcommented, Feb 13, 2021

we use a simple trick to do this in our project

export default ({ mode }) => {
    require('dotenv').config({ path: `./.env.${mode}` });
    // now you can access config with process.env.{configName}

    return defineConfig({
        /* ... */
    })
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Env Variables and Modes - Vite
Vite exposes env variables on the special import.meta.env object. Some built-in variables are available in all cases: import.meta.env.MODE : {string} the mode ...
Read more >
How to load environment variables from .env file using Vite
If you are trying to access env vars outside your app source code (such as inside vite.config.js ), then you have to use...
Read more >
How to use environment variables in Vite - Koen Woortman
First, you can access your environment via import.meta.env . This returns an object containing the available environment variables known to Vite ...
Read more >
Vue 3 + Vite - Access Environment Variables from dotenv (.env)
There's only one gotcha, to access environment variables from the client app they must be prefixed with VITE_ . Otherwise they will only...
Read more >
Environment variables - Vue.js Developers
If you aren't aware, Vue CLI 3 will pass in any variables you specify in the file .env into your config file. So...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found