Vite 5: Staging Builds Force import.meta.env.PROD=true Despite Custom MODE
0 reputation · 02 Feb 2025, 18:05 UTC
0 reputation · 02 Feb 2025, 18:05 UTC
Vite 5 couples the build command to import.meta.env.PROD === true regardless of the --mode flag. When running vite build --mode staging, import.meta.env.MODE correctly resolves to "staging", yet import.meta.env.PROD remains true and import.meta.env.DEV stays false.
Applications that gate production-only code paths behind if (!import.meta.env.PROD) checks cannot exercise those branches during staging validation. The current design offers no configuration option to decouple PROD from the build command while retaining production-grade optimizations (minification, hashing, etc.).
Determine whether a staging build can simultaneously:
import.meta.env.MODE === "staging"import.meta.env.PROD === false for feature-flag evaluationIs there an existing vite.config.ts pattern, plugin hook, or define override that achieves this without forking the build pipeline? Does Vite's internal importMetaEnv plugin expose a mechanism to customize the PROD/DEV derivation per mode?
No. In Vite 5, vite build always sets import.meta.env.PROD to true and import.meta.env.DEV to false, independent of --mode. Running vite build --mode staging gives import.meta.env.MODE === 'staging' while PROD stays true. Vite 5 has no built-in vite.config.ts option, supported define override, or public importMetaEnv mechanism that flips PROD per mode while retaining production minification and hashing.
import.meta.env.MODE follows --mode and matching .env.[mode] files.import.meta.env.PROD is derived from the build command context, not from MODE; for vite build it is true.VITE_-prefixed variables are exposed to client code, so .env files cannot override PROD, DEV, SSR, or BASE_URL.The importMetaEnv transform injects a static environment object and exposes no public option to recompute PROD or DEV per mode. A define override on import.meta.env.PROD is not a supported API and may break in future versions because Vite replaces the member expression before generic substitution.
First decide whether you can change application source. That choice determines the approach.
Gate feature flags on MODE or a custom variable instead of PROD.
// .env.staging
VITE_STAGING=true
const isStaging = import.meta.env.MODE === 'staging';
const isProduction = import.meta.env.MODE === 'production';
if (isStaging) { /* staging validation path */ }
Use a Vite plugin transform hook to rewrite import.meta.env.PROD for the staging build. This is custom code, not a built-in mechanism.
import { Plugin } from 'vite';
export function stagingProdOverride(): Plugin {
return {
name: 'staging-prod-override',
enforce: 'pre',
transform(code, id) {
if (id.includes('node_modules')) return null;
if (code.includes('import.meta.env.PROD')) {
return code.replace(/import\.meta\.env\.PROD/g, "import.meta.env.MODE === 'production'");
}
return null;
}
};
}
Add the plugin only for staging builds, then run:
npx vite build --mode staging
grep -R 'import.meta.env.PROD' dist/ | head -n 20
grep -R 'MODE === production' dist/ | head -n 20
Confirm the emitted bundle contains the rewritten expression and no literal true substitution where staging code should run.
Can you modify the application source to replace import.meta.env.PROD checks, or must the build rewrite them for you? This determines whether Option 1 or Option 2 applies.
PROD to a MODE comparison can change dependency tree-shaking; staging assets may not be byte-identical to a true production build.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.