.env · generator · React (Vite)

React (Vite) .env Generator

Vite uses VITE_ prefixes to decide which variables ship to the browser. This generator produces a React .env with the right prefixes and the most common third-party client keys (Supabase, PostHog, Sentry).

.env · React (Vite) · 6 keys
Customize →
VITE_API_URL=http://localhost:8000
VITE_APP_NAME=My App
VITE_SUPABASE_URL=
VITE_SUPABASE_ANON_KEY=
VITE_POSTHOG_KEY=
VITE_SENTRY_DSN=

Secrets regenerate on every page load. Want to combine stacks? Use the full generator.

How to use this .env in React (Vite)

  1. 1.Save the output as .env or .env.local in your Vite project root.
  2. 2.Access values with import.meta.env.VITE_API_URL (not process.env).
  3. 3.Restart the dev server after editing — Vite inlines VITE_ values at build time.
  4. 4.For production, set the same keys in your host's dashboard and rebuild.

Gotchas

  • ⚠Non-VITE_ variables are invisible to your React code entirely — prefixing is mandatory.
  • ⚠Vite inlines values at build time. Changing a value in the host dashboard requires a redeploy.
  • ⚠Never put a secret (Stripe sk_, database passwords) behind a VITE_ prefix — it ships to every browser.

Common keys explained

VITE_API_URL

Your API base URL. Safe to expose — it's a URL.

VITE_APP_NAME

Displayed app name. Pure UI config.

VITE_SUPABASE_URL

Supabase project URL. Safe.

VITE_SUPABASE_ANON_KEY

Supabase public anon key. Safe — RLS protects data.

VITE_POSTHOG_KEY

Analytics write key. Safe to expose.

VITE_SENTRY_DSN

Sentry public DSN. Safe to expose.

Loading and validating the .env in React (Vite)

src/config.ts
// Vite: only VITE_* variables reach the browser bundle
export const config = {
  apiUrl: import.meta.env.VITE_API_URL,
  sentryDsn: import.meta.env.VITE_SENTRY_DSN,
  mode: import.meta.env.MODE, // "development" | "production"
};

// Type them once (src/vite-env.d.ts):
// interface ImportMetaEnv { readonly VITE_API_URL: string }

// Create React App (deprecated) used REACT_APP_* and process.env:
// const apiUrl = process.env.REACT_APP_API_URL;

React (Vite): env vars in development, staging and production

A React SPA has no server at runtime. Every variable is replaced with its literal value when you run vite build, so anything in a VITE_ variable ends up readable in the shipped JavaScript. Treat all of them as public. Real secrets belong in your backend or a serverless function.

Vite loads .env, .env.local, .env.[mode] and .env.[mode].local, and mode-specific files win. Use vite build --mode staging with a .env.staging file to produce a staging bundle. One build per environment is normal for SPAs.

React (Vite): .env errors and fixes

import.meta.env.API_URL is undefined

Vite only exposes variables with the VITE_ prefix. Rename it to VITE_API_URL and restart the dev server.

Values are stale after editing .env

Vite reads .env on startup. Restart npm run dev after every change.

process is not defined

Code copied from Create React App uses process.env. In Vite, replace it with import.meta.env.

React (Vite) .env FAQ

Can I hide an API key in a React .env file?

No. Anything the browser needs ends up in the bundle and can be read in DevTools. Proxy the call through a backend that holds the key.

VITE_ vs REACT_APP_ prefix?

VITE_ is for Vite projects (today's default). REACT_APP_ was Create React App, which is deprecated. Next.js uses NEXT_PUBLIC_. The prefix tells the bundler which variables are allowed into client code.

How do I use different API URLs for staging and production?

Create .env.staging and .env.production with different VITE_API_URL values and build with --mode staging or the default production mode.

Related tools

Other stacks