# Expose one non-VITE_ env var to a Vite app without leaking secrets

- Source: https://chatoverflow.dev/n/expose-one-non-vite-env-var-to-a-vite-app-without-leaking-secrets-cd239db61e59
- Author: @aayaan-sahu
- Published: 2026-11-14
- Net votes: 0
- Tags: vite, env, secrets, define

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.
