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:
- Only names that start with
VITE_are visible in the browser, asimport.meta.env.VITE_SOMETHING. A variable namedSUPABASE_URLcan exist on the server and still beundefinedin your app. - Changing a value in the Netlify UI does nothing until you rebuild. The old deploy still contains the old (or empty) values.
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
- In Bolt, open the project files and find
.env,.env.local, or whatever the preview was using. - Search the repo for
import.meta.envso you do not miss a name that never made it into the env file. - Write down the names exactly, including capital letters and the prefix.
VITE_SUPABASE_ANON_KEYandVITE_SUPABASE_PUBLISHABLE_KEYare not interchangeable unless your code reads both.
Step 2: add them in Netlify, then rebuild
- Open the site in Netlify > Site configuration > Environment variables.
- Add each name and value. If your plan shows scopes, include Builds (and Production, plus Preview if you want deploy previews to work).
- 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
- The live file at a hashed path under
/assets/still contains the stringundefinednext to your client setup. That means this deploy did not see the variable. Clear cache and deploy again. - You set the variable on the wrong site or team, or only on a branch deploy.
- The code reads
process.env.VITE_...in a Vite app. In the browser it should beimport.meta.env.VITE_.... - Supabase auth still points at the Bolt preview URL. After the env is fixed, add the Netlify domain to the redirect list (see login redirects to localhost).
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
- Bolt.new deploy failed (or broken) on Netlify: read the log and fix the real error
- Exposed API keys in a Vite / React bundle: why VITE_ variables are public and how to fix it
- Lovable or Bolt app returns 404 on refresh: the SPA fallback for every host
- Netlify build fails with "Secrets scanning found secrets in build": tell a public key from a real leak
- Next.js environment variables undefined on Vercel: NEXT_PUBLIC_, redeploys and "supabaseUrl is required"