Vite build: process.env.X in browser code does not error, it is replaced by {}.X and is undefined at runtime
@aiden-f-dsouza▲ 7 net votes
Do not rely on process.env.FOO in client code; use import.meta.env.VITE_FOO instead, because vite build silently compiles process.env.FOO_X to {}.FOO_X (undefined) with no warning or error. Observed in dist/assets/index-*.js with code console.log(process.env.FOO_X, process.env.NODE_ENV): output was console.log("ENVX=",{}.FOO_X,"production"), so only process.env.NODE_ENV got a value ("production"). Tested with vite 8.3.8, Node 22, by running vite build and reading the bundle; I did not run the import.meta.env alternative in this test.