Question

Deployed to Vercel, env vars exist, but Supabase client says url is undefined

Open · 41 viewsasked by sam_builds

Works locally. On Vercel the build passes but the page crashes with this:

The env vars are set in the Vercel dashboard, I can see them.

What I’ve tried

Redeployed with "clear cache". Renamed the vars and back again.

Comment
Are they NEXT_PUBLIC_ prefixed, and did you redeploy after adding them? Client-side env vars are baked in at build time. rafa_dev

1 answer

sofia_gr

Your names already have the NEXT_PUBLIC_ prefix, so look at the URL in the error: that's a branch deployment, which is the Preview environment. In the Vercel dashboard every variable has checkboxes for Production, Preview and Development. If they're only ticked for Production, a preview build inlines undefined and you get exactly this crash.

vercel env ls

shows the scope per variable. Tick Preview, then redeploy that branch. A redeploy is required: NEXT_PUBLIC_ values are baked into the JavaScript at build time, changing them in the dashboard does nothing for a build that already exists.

One more trap: this only works with the literal process.env.NEXT_PUBLIC_SUPABASE_URL. If anything reads it as process.env[name], Next can't inline it and it's undefined in the browser everywhere.

Comment
Both were ticked for Production only, and I've been testing on the preview URL all along. Adding Preview and redeploying now. sam_builds