Fix Environment Variables Not Working on Netlify
Also appears as:
- Environment variable is undefined
- Cannot read properties of undefined (reading 'VARIABLE_NAME')
- Error: Missing required environment variable
- API key not found in environment
Symptoms
- Environment variables work locally but not on Netlify
- API calls fail because keys are undefined
- Build succeeds but the site shows missing data
- Some environment variables work but others don't
What Causes This Error
Variable not set in Netlify dashboard
CommonThe most common cause: the variable exists in your local .env file but was never added to Netlify's environment variable settings. Netlify doesn't read your .env files.
Missing framework prefix for client-side access
CommonMost frameworks require a special prefix to expose variables to the browser. Next.js uses NEXT_PUBLIC_, Astro uses PUBLIC_, SvelteKit uses PUBLIC_, and Vite uses VITE_. Without the prefix, the variable is only available server-side.
Variable set in wrong scope
OccasionalNetlify lets you scope variables to specific deploy contexts (production, deploy-preview, branch-deploy). If your variable is scoped to production only, it won't be available in deploy previews.
Variable value contains special characters
OccasionalVariables with quotes, newlines, or special characters may not be parsed correctly. This is especially common with multi-line keys (like Firebase service account JSON).
Build cache serving stale values
RareAfter changing an environment variable, the build cache might still hold the old value. This is rare but can cause confusion when debugging.
How to Fix It
Fix 1: Add the variable in Netlify dashboard
- Go to your site in Netlify
- Navigate to Site settings > Environment variables
- Click 'Add a variable'
- Enter the variable name and value
- Select the appropriate scope (all deploys, production, preview, etc.)
- Trigger a new deploy
Verify: Redeploy and check if the variable is now accessible in your build logs.
Fix 2: Add the correct framework prefix
- Identify which framework you're using
- Rename the variable with the appropriate prefix
- Update your code to reference the prefixed variable name
- Update both your local .env file and Netlify dashboard
Next.js: NEXT_PUBLIC_API_URL=...
Astro: PUBLIC_API_URL=...
SvelteKit: PUBLIC_API_URL=...
Vite/React: VITE_API_URL=...
Gatsby: GATSBY_API_URL=...
Nuxt: NUXT_PUBLIC_API_URL=...Verify: Check the browser console — the variable should now be defined in client-side code.
Fix 3: Check and fix variable scope
- Go to Site settings > Environment variables in Netlify
- Check the scope of each variable
- Ensure variables needed in deploy previews aren't scoped to production only
- For sensitive keys, use different values per scope
Verify: Test the variable in both production and deploy preview environments.
Fix 4: Handle special characters in values
- For multi-line values (like JSON keys), base64 encode the value
- Decode it in your application code at runtime
- Alternatively, use Netlify's UI which handles multi-line values better than the CLI
# Encode
cat service-account.json | base64
# In your code, decode:
const key = Buffer.from(process.env.SERVICE_ACCOUNT_BASE64, 'base64').toString()Verify: Verify the decoded value matches the original in your build logs (be careful not to log secrets).
Fix 5: Clear cache and redeploy
- Go to Deploys in your Netlify dashboard
- Click 'Trigger deploy'
- Select 'Clear cache and deploy site'
- Wait for the build to complete
Verify: If the variable works after cache clearing, the issue was stale cached values.
How to Prevent This
Keep a .env.example file in your repo listing all required variables (without values) so new team members know what to set.
Validate environment variables at build time — fail fast with a clear error message if required variables are missing.
Use Netlify's team-level environment variables for shared secrets across multiple sites.
Use the Netli.fyi app to quickly check your site's environment variable configuration.
Document which variables need the client-side prefix in your project's README.

Developer Advocate at RevenueCat and creator of Netli.fyi. Building on Netlify since 2019. Writes from hands-on experience deploying dozens of production sites.
Manage Netlify on the go
Download Netli.fyi and monitor your sites, check deploys, and manage your projects from anywhere.