NEXT_PUBLIC_ and VITE_ variables: why your secret keys are public
Putting a key in an environment variable feels safe. But if the variable name starts with a public prefix, your framework copies its value into the JavaScript that every visitor downloads.
How public prefixes work
- Next.js: variables starting with
NEXT_PUBLIC_are inlined into the browser bundle at build time. Others are only available on the server. - Vite (used by many Lovable and Bolt projects): only variables starting with
VITE_are exposed throughimport.meta.env, and they end up in the client bundle. - Create React App:
REACT_APP_variables are embedded in the build. - Expo:
EXPO_PUBLIC_variables are embedded in the app.
The prefix exists so you can opt in to exposing a value. It's a label meaning "this is safe for everyone to see," not a hiding place.
How to check, step by step
- Open every
.env*file and your hosting dashboard's environment settings. - List every variable with a public prefix. For each one ask: would I be OK posting this value on X?
- Red flags: names containing
SECRET,SERVICE_ROLE,PRIVATE, or values starting withsk_,sk-,sk-ant-. - Confirm on the live site: open DevTools, search all loaded scripts for part of the value.
How to fix it
# before (leaks to every visitor)
NEXT_PUBLIC_OPENAI_API_KEY=sk-proj-...
VITE_STRIPE_SECRET_KEY=sk_live_...
# after (server only)
OPENAI_API_KEY=sk-proj-...
STRIPE_SECRET_KEY=sk_live_...
Renaming alone isn't enough. Move every place that uses the key into server code (an API route, server action or edge function), because a pure Vite single-page app has no server. Then rotate the key, since the old value is already in deployed bundles.
Copy-paste prompt for your AI coding tool
Paste this into Cursor, Claude, Lovable, Bolt or whichever AI built your app:
Find every environment variable in this project that uses a browser-public prefix (NEXT_PUBLIC_, VITE_, REACT_APP_, EXPO_PUBLIC_). For each one, tell me if the value is safe to be public. For any secret: rename it without the prefix, move all code that uses it into server-only code (API route, server action or edge function), update my hosting env settings, and list the keys I must rotate because they were already exposed.
Check your repo in about a minute
Paste a public GitHub repo into KeySnare for a free grade and your top 3 issues, each with a copy-paste AI fix prompt.
Run a free scan →FAQ
Are NEXT_PUBLIC_ variables secret?
No. Next.js inlines NEXT_PUBLIC_ values into the JavaScript sent to the browser, so anyone can read them.
Are VITE_ variables visible in the browser?
Yes. Vite exposes VITE_-prefixed variables to client code through import.meta.env, and they are included in the built bundle.
Can I hide a secret in a Vite single-page app?
Not in the browser code. You need a server-side function (for example a Supabase Edge Function or serverless function) to hold the secret and make the API call.
Do I need to rotate a key after removing the prefix?
Yes. The old value is already in deployed bundles and possibly cached copies, so create a new key and revoke the old one.