Bolt.new works in preview but breaks on Netlify: missing environment variables

By AI App Medic · Updated · Bolt · Netlify · Environment variables · Vite

The Bolt preview looks perfect. You deploy to Netlify and get a blank page, a console error like supabaseUrl is required, or a screen that prints undefined for your project URL. This is almost never a random Netlify outage. Bolt's preview runs inside a WebContainer that already has your .env. Netlify builds the project on its own machines and does not receive those values until you add them there and run a fresh build.

This is a different problem from a build that never finishes. If the deploy log is red, start with Bolt.new deploy failed on Netlify. If the deploy is green and the live site is wrong, keep reading.

What Vite actually does with env values

Most Bolt apps are Vite + React. Vite reads environment variables while it builds, then writes the values into the JavaScript it ships. Two rules follow from that:

The usual Bolt + Supabase set looks like this in .env:

VITE_SUPABASE_URL=https://your-project.supabase.co
VITE_SUPABASE_ANON_KEY=eyJ...
# newer templates may use VITE_SUPABASE_PUBLISHABLE_KEY instead

The anon or publishable key is designed to be public, as long as Row Level Security is correct. A service_role key, Stripe secret, or OpenAI key is not. If one of those has a VITE_ prefix, treat it as leaked and follow this guide before you go further.

Step 1: get the exact names the code reads

  1. In Bolt, open the project files and find .env, .env.local, or whatever the preview was using.
  2. Search the repo for import.meta.env so you do not miss a name that never made it into the env file.
  3. Write down the names exactly, including capital letters and the prefix. VITE_SUPABASE_ANON_KEY and VITE_SUPABASE_PUBLISHABLE_KEY are not interchangeable unless your code reads both.

Step 2: add them in Netlify, then rebuild

  1. Open the site in Netlify > Site configuration > Environment variables.
  2. Add each name and value. If your plan shows scopes, include Builds (and Production, plus Preview if you want deploy previews to work).
  3. Go to Deploys > Trigger deploy > Clear cache and deploy site. A normal retry can reuse a cached build that never saw the new values.

With the Netlify CLI, from a checkout that has the production .env (careful not to upload secrets you do not need):

npx netlify-cli env:set VITE_SUPABASE_URL "https://your-project.supabase.co"
npx netlify-cli env:set VITE_SUPABASE_ANON_KEY "your-anon-key"

To confirm the build actually received a variable, you can temporarily change the build command so the log prints whether it is set, without printing the value:

[ -n "$VITE_SUPABASE_URL" ] && echo "VITE_SUPABASE_URL is set" || echo "VITE_SUPABASE_URL is MISSING"; npm run build

Remove that echo once you have seen is set in a production deploy log.

Step 3: make sure Netlify is building the app at all

If you dragged the whole project folder onto Netlify Drop, there was no build, so no chance to bake variables in. For a typical Bolt app, Netlify should run npm run build and publish dist:

# netlify.toml
[build]
  command = "npm run build"
  publish = "dist"

For a manual drop, build on your machine with the production values in your local env, then upload only the dist folder. And add a SPA fallback so refresh does not 404, as in this rewrite guide.

Step 4: keep secrets on the server

Need to call OpenAI, Stripe, or anything with a secret? Do not add VITE_OPENAI_API_KEY. Add an unprefixed variable (for example OPENAI_API_KEY) and read it only from a Netlify Function or an Edge Function:

// netlify/functions/ask.js
export async function handler(event) {
  const key = process.env.OPENAI_API_KEY;
  if (!key) return { statusCode: 500, body: "Missing OPENAI_API_KEY" };
  // call the provider here, return only the safe result
}

The browser calls /.netlify/functions/ask. The secret never appears in dist.

Quick checks when it is still undefined

When the console is clean, run a scan of the live URL and confirm you did not ship a secret in a VITE_ name.

FAQ

Why is the variable set in Netlify but still undefined in the app?

Vite only exposes names that start with VITE_ to the browser, and only at build time. A name like SUPABASE_URL, or a variable added after the last deploy, stays undefined in the live JavaScript.

Does Netlify read my .env file from the repo?

Not during its own build in the way Bolt's preview does. Put production values in Netlify's environment UI (or the CLI). Avoid committing secret .env files.

Can I put my OpenAI or Stripe secret in a VITE_ variable so the app can call the API?

No. Anything with a VITE_ prefix is copied into public JavaScript. Use a Netlify Function or a Supabase Edge Function and keep the secret unprefixed.

Related guides