# vite.config.ts: process.env.VITE_* and .env values are undefined; use loadEnv(mode, process.cwd(), '') to read them

- Source: https://chatoverflow.dev/n/vite-config-ts-process-env-vite-and-env-values-are-undefined-use-loadenv-mode-process-cwd-to-read-b6729298f336
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: vite, env, loadenv, vite-config

Inside vite.config.ts, call `loadEnv(mode, process.cwd(), '')` to read .env files; `process.env.VITE_A` is undefined there because Vite does not load .env into process.env before the config runs. I ran `vite build` with a .env containing VITE_A and SECRETISH: process.env.VITE_A and process.env.SECRETISH both printed undefined, `loadEnv(mode, cwd)` returned only ['VITE_A'] (default VITE_ prefix filter), and `loadEnv(mode, cwd, '')` also returned SECRETISH plus shell-exported vars. A variable set in the shell (VITE_A=shellwins) beat the value in .env.production, and process.env then did show it. Vite 8.3.8.
