GUIDES · ENVIRONMENT VARIABLES
process.env is undefined on Vercel: how to find the missing variable
Your env variable works locally and is undefined on Vercel. Where Vercel reads variables from, why NEXT_PUBLIC values need a rebuild, and how to list what your code expects.
Where Vercel reads variables from
Vercel never reads .env, .env.local or .env.production from your repository. Everything comes from the project settings under Environment Variables, scoped per environment: Production, Preview and Development. A variable added for Production only is undefined in every Preview deployment.
NEXT_PUBLIC variables are inlined at build time
Anything prefixed NEXT_PUBLIC_ is replaced in the client bundle when next build runs. Adding the variable after the build and redeploying from cache keeps the old undefined value. Use Redeploy without build cache, or push a commit.
Module scope throws kill the whole build
A client created at module scope, for example new Stripe(process.env.STRIPE_SECRET_KEY), runs during the build for every statically rendered page. When the key is missing the build fails with a stack trace that points at the SDK, not at the variable. Wrap such clients in a function and create them lazily inside the request handler.
let client: Stripe | undefined;
export function stripe() {
const key = process.env.STRIPE_SECRET_KEY;
if (!key) throw new Error("STRIPE_SECRET_KEY is not set");
return (client ??= new Stripe(key));
}List what your code expects
Search the repository for process.env. and compare the names against .env.example and against the Vercel project. DeployDoctor does that diff from the repo URL and lists every variable the code reads that .env.example does not document.
Updated 2026-10-05