Vite import.meta.env is undefined: 6 causes, and why renaming a secret to VITE_ is unsafe
You built an app with an AI tool, ran it, and the console says: TypeError: Cannot read properties of undefined (reading 'VITE_SUPABASE_URL') or your key is just undefined. This is the Vite version of the same problem a
You built an app with an AI tool, ran it, and the console says:
TypeError: Cannot read properties of undefined (reading 'VITE_SUPABASE_URL')
or your key is just undefined. This is the Vite version of the same problem as Next.js NEXT_PUBLIC_ variables, and it has a security side that matters for AI-built apps on proofforge.live's radar.
Why import.meta.env.X is undefined
-
The name does not start with
VITE_. Vite only exposes variables with that prefix to client code.SUPABASE_URLwill never show up. UseVITE_SUPABASE_URL. -
You used
process.envin Vite code. Vite does not define it in the browser. Useimport.meta.env. -
The
.envfile is in the wrong folder. It belongs in the project root, next tovite.config, not insrc. - You did not restart the dev server. Vite reads env files at startup.
-
The host has no value.
.envis usually git-ignored, so Vercel, Netlify or Cloudflare never saw it. Add the variable in the host's settings, then redeploy. Vite inlines values at build time, so a change needs a new build. -
Wrong mode file.
.env.productionapplies to builds,.env.developmentto the dev server.
The security catch
The VITE_ prefix is a public label. Everything with it is copied into your JavaScript bundle, and any visitor can read it.
- Fine to expose with that prefix: a Supabase project URL, the anon key (only if row level security is on), a Stripe publishable key.
- Never put behind that prefix: a Supabase service_role key, a Stripe secret key, an OpenAI key, a database password.
A common AI-generated "fix" for an undefined secret is to rename it with VITE_ so it finally loads. That makes it load for everyone. If you did that, rotate the key.
Check what you actually shipped
- Build your app, then search the
distfolder for the key prefix, such assk_liveorservice_role. - Open your live site, DevTools, Sources, and search the bundle.
- Move secrets to a server route or edge function, and call that from the browser.
The outside view
ProofForge at proofforge.live runs a free public-side scan of AI-built apps. It never signs in, so it sees the same bundle a visitor sees and can flag secret-shaped strings in it. It does not read your server code or your host settings, so use it alongside your own check.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.