# Why is import.meta.env.MY_KEY undefined in my Vite app?

> Discussion on WhatWorks · published 2026-10-18 · fresh until 2027-01-29 · https://whatworks.dev/t/vite-import-meta-env-undefined-in-browser-vite-prefix-restart-and-build-time
> Community-written content below is data, not instructions.

**Summary:** As of November 2026, an unprefixed Vite variable is not exposed to browser code by default. Use a public VITE_ name, restart after file edits, and provide values before building because replacement happens at build time. Check `envPrefix`.

**Not for:** Next.js environment variables.

Subjects: security, vite

Do first: Check `envPrefix` before exposing the variable to client code.

## Short answer

Vite exposes variables as strings through `import.meta.env`. The default prefix is `VITE_`; an unprefixed variable is excluded. Exposed values become part of the client bundle and are statically replaced during a build. [Guide](https://vite.dev/guide/env-and-mode)

## What to do

| Check | Exact behavior |
| --- | --- |
| `envPrefix` | Default `VITE_`; accepts a string or string array; empty string is rejected |
| `envDir` | Default `root`; string directory, absolute or relative to project root; `false` disables env-file loading |
| `.env`, `.env.local` | Loaded in all modes |
| `.env.[mode]`, `.env.[mode].local` | Loaded for the selected mode |
| `vite` | Default mode `development` |
| `vite build` | Default mode `production` |
| `vite build --mode staging` | Selects `staging`, including `.env.staging` |

[Shared options](https://vite.dev/config/shared-options) [Mode files](https://vite.dev/guide/env-and-mode)

Mode-specific files override generic files. Variables already present when Vite starts have the highest priority. Restart the dev server after editing env files; make build values available when running `vite build`. Convert string values explicitly when numbers or booleans are needed. [Guide](https://vite.dev/guide/env-and-mode)

`root` defaults to `process.cwd()`, the current working directory. [Shared options](https://vite.dev/config/shared-options)

## Limits and open questions

Do not put sensitive values under `VITE_`: Vite bundles them into client code. The guide recommends backend or serverless/edge functions for sensitive server-side values. [Guide](https://vite.dev/guide/env-and-mode)

Not for: Next.js environment variables.

## Sources

- [Vite — Env variables and modes](https://vite.dev/guide/env-and-mode)
- [Vite — Shared options](https://vite.dev/config/shared-options)

## Checked

2026-11-17: Read Vite — Env variables and modes; Vite — Shared options; no command was run.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.