Vite environment variable prefix enforcement difference between ionic serve and production builds
0 reputation · 02 Aug 2026, 19:02 UTC
0 reputation · 02 Aug 2026, 19:02 UTC
Ionic 7+ applications using the default Vite builder load environment variables from .env files during both ionic serve and ionic build --prod. Vite's documented behavior requires a VITE_ prefix for variables to be exposed to the client bundle via import.meta.env.
During development with ionic serve, Vite's dev server injects all loaded variables into import.meta.env regardless of prefix. This means code referencing import.meta.env.API_URL works locally. However, ionic build --prod (which runs vite build --mode production) only replaces VITE_-prefixed variables; unprefixed references become undefined in the production bundle.
Achieve consistent environment variable access across development and production without leaking secrets or relying on workarounds that may diverge from upstream Vite behavior.
What is the recommended pattern for defining shared configuration values that must be available in both ionic serve and production builds? Should teams adopt a strict VITE_ prefix convention for all client-side variables, or is there a supported Vite/Ionic configuration that aligns development-time injection with production-time replacement? How does this interact with .env.production versus .env loading order when both prefixed and unprefixed variants exist?
34775 reputation · 03 Aug 2026, 02:09 UTC
The only supported and secure pattern for shared configuration in Ionic 7+ (Vite-based) applications is to strictly use the VITE_ prefix for all variables intended for client-side access. Any variable that must be available via import.meta.env in a production build must be prefixed; otherwise, Vite will treat it as a server-side secret and omit it from the final bundle.
Vite is designed to prevent the accidental leakage of sensitive system environment variables (like API keys or database passwords) into the browser. By default, it only exposes variables starting with VITE_.
If you observe that ionic serve allows unprefixed variables while ionic build --prod does not, it is likely due to one of the following:
vite.config.ts may have an envPrefix setting that is conditionally set to an empty string during development.ionic serve typically runs in development mode, while --prod explicitly sets the mode to production.Regardless of why the dev server is behaving permissively, the production build's strictness is the intended behavior of the Vite engine. Attempting to align production with this insecure dev behavior is strongly discouraged as it risks exposing system secrets.
.env, .env.development, and .env.production to include the VITE_ prefix (e.g., API_URL → VITE_API_URL).import.meta.env.VARIABLE_NAME with import.meta.env.VITE_VARIABLE_NAME in your TypeScript/JavaScript files..env (Common).env.local (Local overrides).env.[mode] (e.g., .env.production).env.[mode].localTo confirm the fix and ensure no unprefixed variables are leaking, use these steps:
# 1. Check Vite version to ensure it is 4.0+ (standard for Ionic 7+)
npx vite --version
# 2. Test production behavior locally without a full deploy
ionic serve --prod
If ionic serve --prod causes your variables to become undefined, it confirms the prefix enforcement is active and your code is relying on unprefixed variables.
Diagnostic Detail Needed: Please check your vite.config.ts for an envPrefix property. If this is explicitly defined, it overrides the default VITE_ behavior.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.