Fix guides · Next.js, Vercel, Netlify ·

Fix Next.js environment variables missing at build time

Next.js build fails because a required environment variable is undefined or not available at build time

Next.js reads environment variables from .env files in the project at build time. .env.local is read on a machine where the file exists, but it is gitignored by default, so it never reaches Vercel or Netlify. Hosting platforms like Vercel and Netlify inject their dashboard variables into the build environment. A build fails when a variable the code expects is missing, empty, or has the wrong value. Variables used in client-side code need the NEXT_PUBLIC_ prefix so they are inlined into the browser bundle; otherwise they are undefined at runtime. The fix is to add the variable in the right place with the exact name and trigger a fresh build.

Free · 3 a day · no signup

Common causes – and how to tell which one you have

1. Variable only exists in a .env.local file that was never committed

How to tell: The build passes locally because .env.local is read, but fails on Vercel or Netlify because .env.local is gitignored by default and never reaches the build. Check whether the file containing the variable is named .env.local and whether it appears in the GitHub repository.

Fix: For public values, move them to a committed .env file. For private values, add the variable in the platform dashboard (Vercel Project Settings, Environment Variables; Netlify Site configuration, Environment variables) for the Production context, then rebuild.

2. Client-side variable missing the NEXT_PUBLIC_ prefix

How to tell: The variable is read in a component that runs in the browser, and it is undefined at runtime (the app crashes or shows 'undefined' in the UI), while server code sees it fine.

Fix: Rename it to NEXT_PUBLIC_<NAME> in the .env file and in the code, restart the dev server, and redeploy. Only safe public values belong in NEXT_PUBLIC_ variables.

3. Name typo or wrong casing

How to tell: The variable is missing or misspelled, so process.env.VAR is undefined. Code that assumes a value then behaves unexpectedly or crashes. The variable may also be set to an empty string, which some code treats as missing.

Fix: Make the name in .env and in the platform dashboard exactly match what the code reads, including case, then rebuild.

4. Variable empty or set for the wrong context

How to tell: The variable exists but has no value, or it is set only for Development or Preview while the production build runs in the Production context.

Fix: Set a valid value for the Production context in the platform dashboard and rebuild.

How to fix it

  1. Find the variable name the code actually reads: search for process.env in the project and note the exact spelling and whether it is in a client component (needs the NEXT_PUBLIC_ prefix) or server code.
  2. Open .env or .env.local in the project and confirm the variable is defined there for local development.
  3. For a client-side value, rename the variable to NEXT_PUBLIC_<NAME> in both the .env file and the code, then restart the dev server so Next.js inlines it.
  4. For Vercel, open Project Settings, then Environment Variables, and add the variable with the exact name and value for the Production environment (and Preview if you test there).
  5. For Netlify, open Site configuration, then Environment variables, and add the variable for the Production context (and Deploy previews if needed).
  6. Do not commit secrets to the repository. Commit only safe public values to .env files. For private values, use the platform dashboard only.
  7. Trigger a fresh build: Vercel redeploys on the next push or via Redeploy; on Netlify use Deploys, then Trigger deploy, then Clear cache and deploy site.
  8. Read the new build log and confirm the undefined-variable or build crash no longer appears.

Using Lovable, Bolt or v0?

In Lovable, Bolt or v0, tell the tool: "My Next.js build fails because an environment variable is undefined. Show me every process.env name the code reads, mark which are used in client components, and make client-side ones NEXT_PUBLIC_ prefixed. Do not hardcode secrets. If a client-side variable contains a private key, do not rename it to NEXT_PUBLIC_ because it would be exposed in the browser bundle." Then add the real values in the Vercel or Netlify dashboard yourself, since the tool cannot see your production secrets.

Using Cursor, Claude Code or Windsurf?

Ask the agent to first list every process.env variable the code reads and show which are in client components, before changing anything. Then have it confirm the exact names in .env and tell you which variables must be added in the Vercel or Netlify dashboard, since the agent cannot read those dashboards. After you add them, have the agent trigger a build and paste the build log showing the error is gone. Do not let it hardcode secret values into the source.

The “fix” to avoid

Adding a variable only to a .env file does not help a Vercel or Netlify build if the file is gitignored (such as .env.local). Hardcoding the value in the source leaks secrets into the repository. Adding 'use client' does not expose a non-prefixed variable to the browser, because Next.js only inlines variables that start with NEXT_PUBLIC_.

Copy-paste fix prompt for your AI coding tool

Paste this into Cursor, Claude Code, Lovable or Bolt:

My Next.js build fails because an environment variable is undefined or missing at build time. Before changing anything, check in this order and report what you find: 1) grep for process.env in the code and list each variable name used, noting which are in client components. 2) Show whether each name exists in .env/.env.local with that exact spelling, and whether client-side ones use the NEXT_PUBLIC_ prefix. 3) Report which hosting platform is used (Vercel or Netlify). For Vercel, confirm the variable exists under Project Settings, Environment Variables, for Production; for Netlify, confirm it under Site configuration, Environment variables, for the Production context. Fix the name mismatch or add the missing NEXT_PUBLIC_ prefix in both code and .env, add any missing production variable in the platform dashboard, then trigger a fresh build and show me the build log proving the error is gone. Do not commit secret values to the repo.
Your error looks a bit different? Paste your exact error and get a diagnosis tailored to your code — free.

Questions people ask

Why does my Next.js app build locally but fail on Vercel?

Local builds read .env and .env.local from your project. Vercel and Netlify inject their dashboard variables into the build environment, and Next.js also loads any plain .env file that is committed to the repository. Commit only safe public values to .env files; store secrets and production-specific values in the platform dashboard. If a variable is missing from both the dashboard and any committed .env file, the build sees it as undefined.

Do I need the NEXT_PUBLIC_ prefix for every variable?

No. Only variables that client-side code needs in the browser require the NEXT_PUBLIC_ prefix. Server-side variables should stay unprefixed so they are never shipped to the browser.

Does Netlify read .env files during a production build?

Yes. Next.js loads committed .env files at build time on any host, including Netlify, and Netlify injects its dashboard variables into the build environment. Commit only safe public values to .env files; use the dashboard for secrets.

How do I confirm a variable reached the build?

Check the build log for the undefined-variable or crash message, and temporarily log whether the value is set (without printing the secret) from a server component during the build.

Related errors

Get fixes like this every week