Vite · VITE0031

Do not pass every loaded env variable to define

Reports Vite define entries that receive the whole result of loadEnv with an empty prefix, or the whole process.env.
VITE0031vite-doctor/vitesecurityerrorNo fix

Run this rule

pnpm vite-doctor . --framework vite --rules vite/define/no-loadenv-all-in-define

Why it matters

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.

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) },
  }
})
Copyright © 2026