Guide
Blank page or undefined env vars after deploy? Fix VITE_ and NEXT_PUBLIC_ variables
Short answer: if your Lovable, Bolt or Cursor app works locally or in the builder's preview but shows a blank page once it's deployed, open the browser Console on the live site. An error like supabaseUrl is required. or a request to a URL with undefined in it means the browser code was built without your environment variables. Four things cause almost all of it:
- The name is missing its prefix. Browser code only gets variables that start with
VITE_(Vite) orNEXT_PUBLIC_(Next.js).¹² - You set the variable but didn't rebuild. Both tools write these values into the JavaScript at build time.¹² Changing them on your host does nothing until you build and deploy again.³⁴
- The variable isn't set on the host at all. Your
.envfile usually isn't committed to git, so the host's build never sees it.¹² - It's set for the wrong environment. Vercel, Netlify and Cloudflare Pages let you give production and preview deploys different values.³⁵⁶
Before you fix any of them, check one thing: if the variable holds a secret (a Supabase service_role or sb_secret_ key, a Stripe sk_live_ key), it must not get a VITE_ or NEXT_PUBLIC_ prefix at all. See Never make a secret "public" below.
Why it works locally and breaks after deploy
On your machine, Vite and Next.js read .env files from the project folder.¹² Your builder's preview usually has those values too. A deployed site is different. Your host clones the repo, runs the build command in a fresh environment, and uploads the result. Only the variables that exist during that build end up in the browser code.
That's the key idea. Vite says its env variables are "statically replaced at build time", and that VITE_* values "are bundled into your source code at build time".¹ Next.js says it inlines NEXT_PUBLIC_ values "at build time" and that after the build, "your app will no longer respond to changes to these environment variables".² So the built JavaScript holds either the real string or undefined, and that's fixed until the next build.
A missing Supabase URL is enough to blank the whole page. createClient(undefined, ...) throws supabaseUrl is required., and if that runs at the top of your app, React never renders anything.⁷
Step 1: Find the error in the browser (2 minutes)
Open the live site, then open DevTools (F12, or Cmd+Option+I on a Mac).
Console tab. Reload and look for the first red error. Common ones:
| What you see | What it usually means |
|---|---|
supabaseUrl is required. |
The Supabase URL variable was undefined at build time |
supabaseKey is required. |
Same, for the publishable / anon key |
Invalid supabaseUrl: Must be a valid HTTP or HTTPS URL. |
The value got in, but it's wrong: a typo, missing https://, or extra quotes |
Cannot read properties of undefined near your config code |
Something read from the env object came back empty |
The first three messages come straight from the supabase-js source.⁷
Network tab. Reload with the Network tab open and look for requests that fail or have undefined in them, like https://yourapp.com/undefined/rest/v1/... or https://undefined/.... When code builds a URL from a missing variable, JavaScript turns it into the text undefined. If your page calls your own API, check that its base URL is the production one and not localhost.
Check the built file (optional). In the Sources tab, open the main JavaScript file and search for your Supabase project URL (.supabase.co). If it isn't there, the value never made it into the build.
If the Console is clean and the page is still blank, the cause may not be env vars at all. Common other causes are a wrong base path or a host that doesn't serve your single-page app on deep links. The free Quick Check tests the deep-link refresh case.
Step 2: Use the right prefix
| Framework | Exposed to the browser | Read it with | Stays server-only |
|---|---|---|---|
| Vite (Lovable, many Bolt apps) | VITE_SUPABASE_URL |
import.meta.env.VITE_SUPABASE_URL |
anything without VITE_ |
| Next.js | NEXT_PUBLIC_SUPABASE_URL |
process.env.NEXT_PUBLIC_SUPABASE_URL |
anything without NEXT_PUBLIC_ |
Vite only exposes variables that start with VITE_. Its docs show an unprefixed DB_PASSWORD coming back as undefined in browser code.¹ Next.js only bundles NEXT_PUBLIC_ variables for the browser. Everything else stays on the server.²
Three Next.js traps:²³
- Read it by its full name.
process.env.NEXT_PUBLIC_SUPABASE_URLgets inlined. A dynamic lookup likeprocess.env[name], orconst env = process.env; env.NEXT_PUBLIC_..., does not. - Names must match exactly, including case, between your code and the host settings.
- Using
/src? Next.js only loads.envfiles from the project root, not from/src.²
In Vite, values always arrive as strings. VITE_FEATURE_ON=false gives you the string "false", which is truthy, so compare it to "true" yourself.¹
Step 3: Put the variables on your host
Your .env file is meant to stay out of git. Vite's docs mark .env.local and .env.[mode].local as git-ignored.¹ Next.js warns that you "almost never want to commit" .env files, and create-next-app adds them to .gitignore.² So when your host builds from the repo, it doesn't get them. Add each browser variable in the host's dashboard:
- Vercel: Project → Settings → Environment Variables. Tick Production and also Preview if you use preview deploys. In the Add Environment Variable form you can also import a
.envfile to add many at once.³ - Netlify: Project configuration → Environment variables. By default one value applies to every deploy context, or you can set separate values for Production, Deploy Previews and Branch deploys. If you've limited a variable's scopes (Pro and Enterprise plans), make sure it includes the Builds scope, because the build is when Vite or Next.js reads it.⁵
- Cloudflare Pages: your Pages project → Settings → Variables and Secrets. You can set variables for both production and preview environments, and they're available at build time.⁶
Lovable is the exception: it commits .env with the VITE_ browser values, but server secrets aren't in the repo. Our guide to deploying a Lovable app to Vercel, Netlify or Cloudflare Pages lists what to set on the host. Bolt keeps its environment variables in the project's .env file. If you're moving a Bolt app, see Bolt.new to production: Netlify, custom domain and the refresh-404 fix.
Copy the names exactly from your local .env, prefix included. Don't wrap values in quotes in the dashboard unless your local value really contains them.
Step 4: Rebuild and redeploy
Setting the variable is only half the fix. Vercel says changes "only apply to new deployments". Its troubleshooting table lists "a change in the dashboard has no effect" with the fix "Redeploy the project".³ Netlify says environment variable changes "require a build and deploy to take effect".⁴ Because Vite and Next.js write the values in at build time,¹² the same goes for Cloudflare Pages and any other host. Trigger a fresh build (redeploy from the dashboard, or push a commit), then hard-reload the live page.
If you use Turborepo, Vercel notes that it leaves env variables out of its cache key unless turbo.json lists them, so it can reuse a build made with different values.³ Clearing the cache on redeploy rules that out.
Step 5: Check the preview vs production scope
Common symptom: the preview URL works and the production domain is blank, or the other way round. That's nearly always scope.
- Vercel: each variable applies to the environments you ticked: Production, Preview (all non-production branches, or one branch), Development, or a custom environment.³ A variable set only for Production is missing from every preview, and the reverse is also true.
- Netlify: a variable can have a different value per deploy context, and site variables override shared team ones. Values in
netlify.tomloverride the UI.⁵ If a value looks right in the UI but the build uses another one, checknetlify.toml. - Cloudflare Pages: production and preview each have their own set of variables.⁶
Vite also picks files by mode: vite build runs in production mode, so it loads .env.production as well as .env and .env.local. A value that exists only in .env.development won't be in a production build.¹
Never make a secret "public"
A VITE_ or NEXT_PUBLIC_ variable is, by design, visible to anyone who opens your site. Vite's docs say these variables "should not contain sensitive information such as API keys" and point you to a backend or edge function for secrets.¹
When a variable comes back undefined, it's tempting to rename SUPABASE_SERVICE_ROLE_KEY to VITE_SUPABASE_SERVICE_ROLE_KEY just to make it work. Don't. Supabase's secret keys (sb_secret_..., and the legacy service_role key) bypass every Row Level Security policy. Supabase says never to put one "in a browser, a shipped application, or source control".⁸ The same goes for Stripe secret keys (sk_live_...) and any other API secret.
If a secret key is already live in your JavaScript, here is how to check and rotate it. If you are also unsure whether a .env file itself is being served, test that too.
What belongs in browser variables for a Supabase app:
# Safe in the browser (still protect your data with RLS)
VITE_SUPABASE_URL=https://your-project.supabase.co
VITE_SUPABASE_PUBLISHABLE_KEY=sb_publishable_... # or the legacy anon key
# Server-only: Edge Functions / server code, never VITE_ or NEXT_PUBLIC_
SUPABASE_SECRET_KEY=sb_secret_...
STRIPE_SECRET_KEY=sk_live_...
The publishable key is designed to be public, and it can only access what your Row Level Security policies allow.⁸ If you already shipped a secret in a browser variable, removing it from the code isn't enough. It's still in the JavaScript of every build you deployed with it, so rotate the key in the provider's dashboard straight away. Supabase's guide on API keys has the steps.⁸ Still on the legacy anon and service_role keys? Here's how to move to publishable and secret keys.
Quick checklist
- Console on the live site shows no
supabaseUrl is required.or similar error - Every browser variable starts with
VITE_(Vite) orNEXT_PUBLIC_(Next.js), and is read by its full name - The same names are set on the host, for Production and (if used) Preview
- On Netlify with scopes, browser variables include the Builds scope
- You redeployed after changing any variable
- No secret (
service_role,sb_secret_,sk_live_) has a public prefix - Your Supabase project URL appears in the built JavaScript, and your secret keys don't
FAQ
Why is import.meta.env.VITE_ undefined in production but fine locally?
Locally Vite reads your .env file. On the host, the build only sees variables set in the host's settings, and .env files usually aren't committed. Add the VITE_ variables on the host for the right environment, then rebuild and redeploy.
I changed an environment variable on Vercel or Netlify and nothing happened. Why? Vite and Next.js write browser variables into the JavaScript at build time, and both Vercel and Netlify only apply variable changes to new deployments. Redeploy to pick up the new value.
Why does my Next.js NEXT_PUBLIC_ variable show as undefined in the browser? Usually the name is misspelled, it's read with a dynamic lookup like process.env[name] (which isn't inlined), it isn't set for the environment you built, or you didn't redeploy after adding it.
Is it safe to put my Supabase key in a VITE_ variable? Only the publishable key (sb_publishable_ or the legacy anon key), and only with Row Level Security on your tables. Never put a secret or service_role key, or a Stripe sk_live_ key, in a VITE_ or NEXT_PUBLIC_ variable, because anyone can read it from your site's JavaScript.
Want a second pair of eyes before launch? The free Quick Check looks at your live URL for leaked keys in your JavaScript, exposed .env files, deep-link refresh, HTTPS and SEO basics in about 20 seconds. The $49 Launch Check goes further and gives you a plain-English fix list with copy-paste prompts for Lovable, Bolt or Cursor. For other things that break right after deploy, see Supabase auth redirects to localhost and the Stripe test mode to live checklist.
Sources
- Vite docs, Env Variables and Modes (
VITE_prefix, static replacement at build time,.envfiles and git-ignored*.local, modes, values are strings, protecting secrets): https://vite.dev/guide/env-and-mode - Next.js docs, Environment Variables (
NEXT_PUBLIC_inlined at build time, frozen after build, dynamic lookups not inlined,.envin.gitignore,/srcfolder note): https://nextjs.org/docs/app/guides/environment-variables - Vercel docs, Environment variables (Production / Preview / Development scopes, changes apply only to new deployments, troubleshooting table): https://vercel.com/docs/environment-variables
- Netlify docs, Get started with environment variables (changes require a build and deploy to take effect): https://docs.netlify.com/build/environment-variables/get-started/
- Netlify docs, Environment variables overview (deploy contexts, scopes, overrides,
netlify.tomlprecedence): https://docs.netlify.com/build/environment-variables/overview/ - Cloudflare docs, Pages Functions bindings: Environment variables (production and preview, runtime and build-time) and Build configuration: https://developers.cloudflare.com/pages/functions/bindings/#environment-variables · https://developers.cloudflare.com/pages/configuration/build-configuration/
- supabase-js source (
supabaseUrl is required.,supabaseKey is required.,Invalid supabaseUrlerrors): https://github.com/supabase/supabase-js - Supabase docs, API keys (publishable vs secret keys, secret keys bypass RLS, never in a browser, rotating a leaked key): https://supabase.com/docs/guides/api/api-keys