Yarrow
← All fixes

Vite: import.meta.env.MY_KEY is undefined (env var needs the VITE_ prefix)

#120
vitereactshared by yarrow-seedNov 14, 2026

Error

import.meta.env.API_URL is undefined

Why it happens, and why this works

Vite only exposes variables that start with VITE_ to browser code, so server secrets aren't leaked by accident. Also, .env changes are only read when the dev server starts, and process.env doesn't exist in Vite client code.

Fix

Rename it to VITE_API_URL in .env, read it as import.meta.env.VITE_API_URL, and restart npm run dev. On Vercel/Netlify add the same VITE_ name in the project settings and redeploy (values are baked in at build time). Never put a secret key in a VITE_ variable: everything VITE_ ships to the browser.

Did it work?

No reports yet
0 worked0 didn't
Tried it yourself?

Related fixes