Quasar v2 Vite migration: client env vars undefined in production build
0 reputation · 15 May 2021, 06:53 UTC
0 reputation · 15 May 2021, 06:53 UTC
Quasar Framework v2 replaced Webpack with Vite as the default build tool. Vite only exposes environment variables prefixed with VITE_ to the browser bundle, and they must be read via import.meta.env rather than process.env. During local development quasar dev may still populate non-prefixed variables, creating a false sense of correctness.
A production build (quasar build) strips unprefixed references, leaving undefined in the output. The same code runs without error in the dev server, so the discrepancy appears only after deployment or when serving the built artifacts locally.
process.env.API_URL and similar unprefixed keys..env files.What is the minimal change set to align the variable naming and access pattern so that a clean production build resolves every client-side reference? Are there any Quasar-specific plugins or quasar.conf.js hooks that can automate the prefix migration without manual search-and-replace? Does the framework provide a build-time validation step that fails fast when unprefixed variables are detected in client code?
The minimal change set requires renaming all client-side environment variables from unprefixed forms (e.g., process.env.API_URL) to be prefixed with VITE_ (e.g., VITE_API_URL) in your .env files, and subsequently updating all client-side code to access them via import.meta.env.VITE_API_URL.
During quasar dev, the development server environment may retain access to process.env for convenience. However, Vite strictly enforces that only variables prefixed with VITE_ are injected into the final browser bundle during quasar build. Unprefixed references are stripped or resolve to undefined in production artifacts.
process.env for configuration variables..env files to include the VITE_ prefix.import.meta.env.VITE_KEY.quasar build and verify the resulting static assets.There is no known Quasar-specific plugin or quasar.conf.js hook that automates this prefix migration or provides a built-in fail-fast validation for unprefixed client references.
To ensure correctness, verify the production build by serving the artifacts locally and logging import.meta.env.VITE_API_URL in the browser console; it must match the value in your .env.production file.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.