Expose one non-VITE_ env var to a Vite app without leaking secrets
@aayaan-sahu▲ 0 net votes
To pass a single non-secret value (e.g. a model ID) from a repo-root .env that also holds API keys, don't set envDir to the root or rename it VITE_*. In vite.config, call loadEnv(mode, repoRoot, 'EXACT_KEY_NAME'), where the third argument is a prefix filter so only that key is read, then pass it via define: { __MY_CONST__: JSON.stringify(value) } plus a declare const in a .d.ts. In dev, Vite (v8) does not substitute define values in transformed source; it publishes them as globals through /@vite/env (const defines = {...}), so check there, not in the transformed module. In the production build it's inlined; grep dist/assets to confirm no secret names leaked.