"process.env is not defined" in Vite? The quick fix that ships every secret to the browser
By Abbi Labs · Last updated 30 November 2026
Scan your app free and see if your live JavaScript is carrying secret keys.
Abbi Labs reads the same JavaScript every visitor downloads and flags Stripe, OpenAI, Anthropic, AWS, GitHub, Google (AIza) and Supabase secret keys, masked. It also checks Supabase tables readable with your public key, exposed .env and .git files, source maps, security headers, CORS, cookie flags and TLS. Read-only. No sign-up for the quick check.
No app handy?
Example result · our deliberately broken demo app
6 issues found: 3 critical, 1 high, 1 medium, 1 low
- Critical: Stripe live secret key in public JavaScript
- Critical: /.env file readable by anyone
- High: Git repository exposed (/.git/HEAD)
The demo's problems are planted on purpose. Paste your own URL above to see yours.
No sign-up. Read-only public-resource and protocol metadata checks. No model prompts or tool execution. Scan only sites you own or are allowed to test.
Run on every deploy or use from Cursor / Claude Code via MCP →
Weekly AI-app security checklist
One short email a week: a practical check for apps built with Supabase, Next.js and AI tools (RLS, leaked keys, headers and more). Unsubscribe anytime.
You move a React app to Vite, or your AI builder pulls in a package written for Node, and the console says ReferenceError: process is not defined or process.env is not defined. Search that error and one answer comes up again and again: add define: { 'process.env': process.env } to vite.config. The error goes away. So does every secret on your build machine, straight into your public JavaScript.
Facts below are from Vite's shared options and config docs and Next.js's next.config.js env reference.
Why that one line leaks everything
Vite's define option does a find and replace at build time. Whatever value you give it gets written into your code in place of the expression. Hand it process.env and Vite serialises the whole object, every environment variable the build process can see, and pastes it in wherever browser code reads process.env.
On your laptop that might only be a few harmless values. On Vercel, Netlify or a CI runner, the build environment usually has the lot: STRIPE_SECRET_KEY, OPENAI_API_KEY, SUPABASE_SERVICE_ROLE_KEY, database URLs with passwords. None of them need a VITE_ prefix to end up in the bundle this way.
The other build-config routes in
- Vite
envPrefix. Vite exposes variables starting with the prefix (defaultVITE_) onimport.meta.env. Setting it to''would expose everything, and Vite throws an error if you try. But adding a second prefix, say['VITE_', 'STRIPE_'], is allowed and quietly exposes everySTRIPE_variable. - One secret in
define.__OPENAI_KEY__: JSON.stringify(env.OPENAI_API_KEY)afterloadEnv(mode, process.cwd(), '').loadEnvitself is fine. Passing a secret from it intodefineis not. - Next.js
envin next.config.js. Next.js calls this a legacy API and says values set this way are always included in the JavaScript bundle. TheNEXT_PUBLIC_prefix only matters for variables from the environment or .env files, so a secret listed underenvships whatever it's called.
The prefix rules themselves (anything starting with VITE_ or NEXT_PUBLIC_ is public) are covered in NEXT_PUBLIC_ and VITE_ variables are shipped to every visitor.
Check yours in 3 minutes
- Read the config. Open
vite.config.ts(or .js) andnext.config.js(or .ts / .mjs). Look for adefineblock that mentionsprocess.envorenv., anenvPrefixsetting, and anenv:block in the Next config. - Search the build output. Run a production build and search what it produced (
distfor Vite,.next/staticfor Next.js):
Also search for the names of your own secret variables, likegrep -rlE "sk_live_|sk_test_|sk-proj-|sk-ant-|service_role|sb_secret_|AKIA|ghp_|postgres(ql)?://" dist .next/static 2>/dev/nullSTRIPE_SECRET_KEY. If a whole env object got pasted in, you'll often see names likePATH,HOMEorVERCEL_sitting in a JavaScript file, which is a giveaway. - Check the live site. Open your deployed site, DevTools, Sources, and search all files (Ctrl/Cmd + Shift + F) for the same strings. What's live is what attackers get, and it can differ from your local build.
The safe fix
Replace the catch-all line with only what the browser genuinely needs:
// vite.config.ts
import { defineConfig } from 'vite'
export default defineConfig(({ mode }) => ({
define: {
// only if a library needs it, and only this one value
'process.env.NODE_ENV': JSON.stringify(mode),
},
}))In your own code, read public values from import.meta.env.VITE_SUPABASE_URL and friends, not process.env. Public means things like your Supabase URL and anon or publishable key, which are designed to be seen (why the anon key is fine).
Anything secret moves to the server. Call Stripe, OpenAI or the Supabase service_role key from an API route, server action or Supabase Edge Function that checks who's asking, and keep the key in an environment variable with no public prefix. In Next.js, delete secrets from the env block and read process.env.STRIPE_SECRET_KEY only in server code.
Then rotate
Removing the line stops the next deploy from leaking. It does nothing about the deploys that already did. Old JavaScript files can linger in browser caches, preview deployments and archive sites, so for every secret that was in the build environment:
- Create a new key in the provider's dashboard (Stripe, OpenAI, Supabase, AWS and so on).
- Put the new key in your host's environment variables and redeploy with the fixed config.
- Revoke the old key once the new one works.
- Check the provider's logs and billing for use you don't recognise.
Supabase specifics are in Supabase service_role key exposed? and AI keys in AI API key exposed in your frontend.
Paste this into your AI builder
Check the build config for anything that copies secrets into browser code.
1. In vite.config, remove any define entry that passes process.env, a
loadEnv() result, or a secret value. If a library needs
process.env.NODE_ENV, define only that one value.
2. Make sure envPrefix is only 'VITE_' (or not set).
3. In next.config, remove secrets from the env block.
4. Browser code may only read import.meta.env.VITE_* (or NEXT_PUBLIC_*)
values that are safe to be public. Move any call that needs a secret
key to a server route or edge function that checks the user.
5. List every env var the browser bundle now reads.
Don't "fix" an undefined variable by adding a public prefix.What Abbi Labs checks
Honest scope: the free quick check downloads your home page and the JavaScript files it loads from your domain and flags secret key patterns it finds there, showing only a masked prefix and suffix. That catches the result of a leaky define when the leaked values include a recognisable key (Stripe live, OpenAI, Anthropic, AWS, GitHub, Google, Supabase service_role or sb_secret_). It doesn't read your config files, can't recognise a secret with no known format, and doesn't crawl other pages. Not sure whether a key you found is meant to be public? Paste it into Is this key safe in the frontend?. The key never leaves the page.
FAQ
Is define: { 'process.env': process.env } safe in vite.config?
No. Vite's define option replaces the expression with the value you give it at build time. Give it the whole process.env object and Vite writes that object, with every variable your build machine has, into the bundle wherever browser code reads process.env. On Vercel, Netlify or a CI runner that usually includes your secret keys.
What's the safe fix for "process.env is not defined" in Vite?
Vite doesn't expose process.env to browser code. For values the browser genuinely needs, use import.meta.env.VITE_SOMETHING. If a library insists on process.env.NODE_ENV, define just that one value, for example 'process.env.NODE_ENV': JSON.stringify(mode). Anything secret has to stay in server code, like an API route or a Supabase Edge Function.
Can I set envPrefix to '' so I don't need the VITE_ prefix?
No. Vite's docs say an empty envPrefix would expose all your env variables, and Vite throws an error if you try. Adding extra prefixes to envPrefix (for example 'STRIPE_') is allowed, and it exposes every variable starting with that prefix to the browser too.
Is loadEnv(mode, process.cwd(), '') dangerous?
Not on its own. Vite's docs use it so values in .env files can shape the config, like a port number. It only becomes a leak if you pass that whole object, or a secret from it, into define.
Does next.config.js env have the same problem?
Yes. Next.js marks the env option as a legacy API and says variables set this way are always included in the JavaScript bundle. The NEXT_PUBLIC_ prefix only matters for variables that come from the environment or .env files, so leaving the prefix off doesn't help here.
I removed the line. Am I safe now?
Only after you rotate. Every deploy built with that config shipped the values, and old JavaScript files can sit in caches, preview deployments and archive sites. Treat anything that was in the build environment as leaked: rotate it, then redeploy.
Does Abbi Labs check for this?
It checks the result, not your config. The free quick check (no sign-up) reads your home page and the JavaScript files it loads from your domain and flags secret key patterns it finds there, masked, including Stripe live, OpenAI, Anthropic, AWS, GitHub, Google (AIza) and Supabase service_role or sb_secret_ keys. It doesn't read vite.config or next.config, can't see secrets with no recognisable format (like a random database password), and doesn't crawl beyond the home page's scripts.