fixmyvibe.codes
All articles

Works in Preview, Not in Production: Environment Variables

7 min read By Ivan Vetrau

environment-variables · staging-environment · deployment · production-bugs

Two blue xylophones sit side by side on a navy schematic, with a green bar on the left and a dashed red outline marking the empty slot on the right.

If your app runs perfectly in Lovable’s preview pane or on your local dev server, but breaks the moment you point your own domain at it, do not start by reading your code. Open the browser console on the live site and look for a value that reads undefined where a key or URL should be, something like a fetch to undefined/api/users or a console error saying a Supabase client could not be created. That single clue, works in preview but not in production, almost always means an environment variable did not travel with your app to its new home. We get this call often: the app ran fine for weeks, then someone connected a custom domain or redeployed, and suddenly login fails, the checkout stays stuck in test mode, or the whole page just goes blank.

What an environment is and why your app has several

An environment is just a specific copy of your app running with its own settings. Most vibe-coded apps end up with at least three without anyone planning it that way.

There is dev, which is your laptop or the AI tool’s built-in preview, usually talking to a test database with made-up data. There is preview or staging, a copy deployed somewhere like Vercel or Netlify so you or a client can click around before it goes live, often still wired to test API keys. And there is production, the version on your real domain, which should be the only one touching real customer data and real payment keys.

The problem is that each of these needs a different set of values: a different database URL, a different Stripe key, a different allowed domain for authentication redirects. Your code is identical across all three. What changes is a list of settings sitting outside the code, usually in a .env file locally and in your hosting provider’s dashboard everywhere else. When that list is incomplete, stale, or copied wrong, the symptom looks like a bug, but the bug is in configuration, not logic. If you want the fuller picture of why this kind of thing slips through in AI-built apps generally, we covered that ground in why a vibe-coded app breaks in production.

The symptoms of a missing variable

A missing or wrong environment variable rarely throws a clean error. It shows up as something else entirely, which is why it gets misdiagnosed as “the AI broke something.”

A few patterns we see constantly:

None of these look like “check your environment variables” on the surface. They look like broken features. That is exactly why this is one of the most common support tickets we get, right alongside the ten most common bugs in AI-generated code.

Public vs secret variables: the VITE_ / NEXT_PUBLIC_ trap

This is the part that trips up almost everyone, technical or not, and it matters for security as well as function.

In frameworks built on Vite, any environment variable prefixed with VITE_ gets bundled straight into the JavaScript that ships to every visitor’s browser. Next.js does the same thing with NEXT_PUBLIC_. The prefix is not decoration, it is a promise to the build tool: “put this where anyone can read it.” That is fine for a Stripe publishable key or a public API URL. It is a serious problem if someone names a secret key VITE_OPENAI_API_KEY or NEXT_PUBLIC_DATABASE_PASSWORD, because that key is now sitting in plain text in your site’s source, visible to anyone who opens dev tools.

We have found live secret keys this way more times than we can count. If you are not sure whether this has already happened to you, it is worth a direct check before anything else in this article:

# In your browser, on the live site:
# View source, or open DevTools > Sources, and search for:
sk_live_
VITE_
NEXT_PUBLIC_

Anything secret, a database password, a private API key, a service role key, should never carry a public prefix. It belongs only on the server side, set in your hosting provider’s environment settings, never in a file that ships to the browser. We wrote a full walkthrough on spotting this in finding exposed API keys in your AI-built app, and it is worth running even if nothing seems broken, because this particular mistake fails silently until someone finds the key.

How to compare environments without reading code

You do not need to read a line of code to catch most of these problems. You need to compare what each environment actually has set, side by side.

Start with your hosting dashboard, whether that is Vercel, Netlify, Amplify, or Cloudflare. Every one of them has an environment variables page per project, usually with a dropdown or tabs for “Production”, “Preview”, and sometimes “Development”. Open it and make three lists, one per environment, of every variable name present. Do not worry about the values yet, just the names.

Now compare the lists. Any variable that exists in Preview but not Production is a likely cause of exactly the “works in preview, not live” symptom. Any variable present in both but pointing to a different service, a test Stripe key in one and a live key in the other, tells you where payments or emails are quietly misbehaving.

A simple table works better than memory here:

VariableDevPreviewProduction
DATABASE_URLtest dbtest dbmissing
STRIPE_SECRET_KEYsk_test_…sk_test_…sk_test_… (should be sk_live_…)
AUTH_REDIRECT_URLlocalhost:3000preview-xyz.vercel.appyourdomain.com

If you cannot even access your hosting dashboard to build this table, that is its own problem, and probably means you do not fully own your deployment yet. That is worth sorting out before anything else, and it is the exact gap we walk through in how to export and own your vibe-coded app.

A simple setup with a staging copy

Once you have untangled the current mess, the fix is not complicated. Set up a proper staging environment that mirrors production as closely as possible, with its own database, its own domain or subdomain like staging.yourapp.com, and its own full set of environment variables, deliberately kept separate from the live ones.

The rule that keeps this clean going forward is simple: every time you add a new environment variable in one environment, add it everywhere else immediately, even if the value is a placeholder for now. A shared checklist like this one, run before every deploy, catches most of what we have described above:

  1. Does production have every variable name that preview has, with no gaps?
  2. Are any secret keys sitting behind a VITE_ or NEXT_PUBLIC_ prefix that should not be public?
  3. Does the live Stripe key start with sk_live_, not sk_test_?
  4. Is the production domain added to every auth provider’s allowed redirect list?
  5. Does the database URL in production point to the real database, not the test one?

We cover a broader version of this same discipline, along with other things worth checking before a public launch, in the vibe coder’s pre-launch checklist. Running through it once, properly, saves a lot of panicked “it worked yesterday” messages later.


If your app works in preview but falls apart on your own domain, send it over. We will tell you exactly which variable is missing and why, free of charge. Get in touch for a free assessment.

Is your own app doing this?

Send us the repo or the app's URL. Within 48 hours we'll tell you its three most serious problems, free.

Get a free code review