Fix guides · Netlify ·
Fix Netlify build failure due to missing environment variable
Netlify build fails because required environment variable not set (e.g. undefined value or crash during build)
Netlify builds read environment variables from the Netlify dashboard (Site configuration → Environment variables) and from netlify.toml under [build.environment] and [context.*.environment]. If a required variable is missing, undefined, or has the wrong value, the build can fail with errors such as undefined variable access or a crash during npm install or build. The fix is to add the variable in the correct place and redeploy.
Common causes – and how to tell which one you have
1. Variable not created in host environment or netlify.toml
How to tell: Build logs show errors about undefined variables or crashes during the build, or the app works locally but fails on deploy.
Fix: Add the variable in your Netlify dashboard's environment variables, or for non-secret values, in netlify.toml under [build.environment] or [context.<context>.environment].
2. Variable name mismatch
How to tell: Code uses process.env.VAR_NAME but environment variable is named differently (case sensitivity, spelling).
Fix: Match the exact variable name in code and environment settings. Check for case sensitivity and spelling.
3. Empty or invalid value
How to tell: Build fails with undefined-variable errors or crashes; runtime API calls may return 401/500 if the variable is used at runtime and is missing or invalid.
Fix: Set the variable to a valid value (not empty string, not placeholder like 'YOUR_KEY'). For secrets, use the host's secure environment variable store.
4. Variable only set locally, not provided to build
How to tell: Works in local dev but fails on Netlify deploy; build logs show variable is undefined in the build environment, or the variable is missing the Builds scope.
Fix: Add the variable in your Netlify dashboard's environment variables or in netlify.toml (for non-secret values). Local .env files are not automatically loaded in the Netlify build environment; frameworks may read committed .env files at build time, but the dashboard is the standard place for production values.
How to fix it
- Open your Netlify dashboard and go to Site configuration → Environment variables. This is where you add, edit, and remove environment variables for your site.
- Check whether the variable your app needs (e.g. API_KEY, DATABASE_URL, STRIPE_SECRET) exists in the list for the correct build context (Production, Deploy previews, Branch deploys, Local development).
- If missing, add it using the variable editor with the exact name (case-sensitive) and correct value. Do not include quotes or extra spaces.
- If the variable exists but has the wrong value, edit it and set the correct value.
- Secrets (API keys, database URLs, tokens) must be stored in the dashboard's secure environment variables — never in netlify.toml, which is committed to the repository.
- If you use netlify.toml for non-secret build-time variables, add them under [build.environment] or [context.<context>.environment]. Note: The netlify.toml key for Local development context is 'dev', not a build-context name.
- If the variable is only set in your local .env file, remember that Netlify builds read environment variables from the dashboard or netlify.toml. Frameworks such as Vite and Next.js read committed .env files at build time if present in the project, but this is not a substitute for the dashboard for secrets.
- After adding or updating the variable, trigger a new build in Netlify (Deploys page → Trigger deploy → Clear cache and deploy site).
- Watch the build logs. If the build fails with errors about undefined variables or crashes during the build, the variable is likely missing or misconfigured. Fix the variable, then redeploy.
Using Lovable, Bolt or v0?
In Bolt, ask: "My Netlify build fails with an undefined-variable error during deploy. Make sure the required environment variable is set in Netlify's dashboard (for secrets) or netlify.toml (for non-secret build-time values), and tell me the exact variable name to add. Then redeploy."
Using Cursor, Claude Code or Windsurf?
Ask the agent to find every place the variable is read (process.env.VAR) and where it's set (.env, host dashboard, shell profile), then show the exact variable name to add in the host's environment settings. Have it add a temporary log that prints whether the variable is defined (without printing its value or length), then re-run the build and confirm the build log shows success instead of the undefined-variable error. Afterwards, remove the log and redeploy.
The “fix” to avoid
Agents often suggest editing .env files only, or adding the variable to the wrong build context (e.g. Development instead of Production), or putting secrets in netlify.toml. These don't work for Netlify builds and expose secrets in the repository.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My Netlify build fails with an error such as an undefined variable access or a crash during build. Check in this order: 1) In the Netlify dashboard → Site configuration → Environment variables, confirm the required variable (e.g. API_KEY, DATABASE_URL, STRIPE_SECRET) exists for Production and has the correct value, and is scoped to Builds (not just Functions/Runtime); 2) For secrets, use the dashboard's environment variables — do not put them in netlify.toml; 3) For non-secret build-time values, you may use netlify.toml under [build.environment] or [context.<context>.environment] (the netlify.toml dev context key is 'dev'); 4) Trigger a new redeploy (Deploys page → Clear cache and deploy) and watch the build logs for the variable name; 5) If still failing, check if the code reads the variable from the wrong place (e.g. process.env.VAR vs process.env.VAR_NAME) or runs in browser instead of server.
Questions people ask
Why does it work locally but fail on Netlify?
Local dev uses your .env file, but Netlify builds use environment variables set in the Netlify dashboard. The .env file is not automatically loaded in the build environment.
Do I need to redeploy Netlify after adding a variable?
Yes. After adding or changing a variable, go to the Deploys page and trigger a new deploy (Clear cache and deploy site). The build reads environment variables at build time, so an existing deployment keeps its old values.
Can I set environment variables in .env.local or a committed .env file?
Netlify reads environment variables from its dashboard and from netlify.toml ([build.environment] / [context.*.environment]). Frameworks such as Vite and Next.js read committed .env files at build time if present. However, for production secrets the dashboard is the secure standard — never put secrets in committed files.