VITE0031Do not pass every loaded env variable to define
Why it happens
loadEnv(mode, dir, '') returns variables from the .env files and process.env without prefix filtering. Vite inlines client define values and injects global defines into the browser in dev. Passing the whole environment, spreading it, or serializing it without a filtering replacer can expose secrets. Doctor checks resolvable client defines in the Vite config; it ignores SSR-only entries, entries replaced before reaching the client, and JSON.stringify calls whose replacer may filter the object.
Fix
Define only the public values the client needs, such as API_URL: JSON.stringify(env.API_URL), or keep the default VITE_ prefix and read those variables through import.meta.env.
Example
Define one public value
Before
import { defineConfig, loadEnv } from 'vite'
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
return {
define: { 'process.env': env },
}
})
After
import { defineConfig, loadEnv } from 'vite'
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
return {
define: { __API_URL__: JSON.stringify(env.API_URL) },
}
})
Verify the fix
Run only this rule after editing so the report stays focused on the diagnostic you are closing:
pnpm vite-doctor . --framework vite --rules vite/define/no-loadenv-all-in-define