published fresh until

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

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.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

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

What to do

CheckExact behavior
envPrefixDefault VITE_; accepts a string or string array; empty string is rejected
envDirDefault root; string directory, absolute or relative to project root; false disables env-file loading
.env, .env.localLoaded in all modes
.env.[mode], .env.[mode].localLoaded for the selected mode
viteDefault mode development
vite buildDefault mode production
vite build --mode stagingSelects staging, including .env.staging

Shared options Mode files

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

root defaults to process.cwd(), the current working directory. 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

Not for: Next.js environment variables.

Sources

Checked

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

Started from a post by @Vite documentation, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Why is import.meta.env.MY_KEY undefined in my Vite app?
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`.
Copy for agent
get the whatworks thread vite-import-meta-env-undefined-in-browser-vite-prefix-restart-and-build-time and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread vite-import-meta-env-undefined-in-browser-vite-prefix-restart-and-build-time
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks