Netlify: env vars not loading in functions [2026 fix]
Env vars undefined in Netlify Functions? Build-time vs runtime confusion. Use netlify.toml [functions] context or redeploy after adding vars.
TL;DR
Cause: Environment variables defined in Netlify UI aren't automatically injected into serverless functions at runtime without explicit configuration in netlify.toml.
Fix: Add [functions] context to netlify.toml or redeploy after updating env vars in Netlify dashboard (cache invalidation).
---
Real Console Error Messages
``` 1. TypeError: Cannot read property 'STRIPE_KEY' of undefined at Object.<anonymous> (/var/task/stripe-webhook.js:15:23)
2. ReferenceError: process.env.DATABASE_URL is not defined at Runtime.handler (/var/task/handler.js:8:5)
3. Error: Missing OPENAI_API_KEY in environment at Object.<anonymous> (/var/task/functions/openai.js:1:1)
4. undefined (Netlify Function returned undefined for env variable access) at console.log (/var/task/api/check-env.js:12:5)
5. SyntaxError: Unexpected token undefined in JSON at position 0 (Returned from function receiving undefined env var in response) ```
---
Broken Code vs. Fixed Code
❌ Broken: Functions not seeing env vars
```javascript // netlify/functions/payment.js exports.handler = async (event) => { const stripeKey = process.env.STRIPE_SECRET_KEY; console.log(stripeKey); // logs: undefined return { statusCode: 200, body: JSON.stringify({ key: stripeKey }) }; }; ```
Problem: Even though STRIPE_SECRET_KEY is set in Netlify UI, functions can't access it.
✅ Fixed: Three approaches
Approach 1: Add functions context to netlify.toml (Recommended)
```toml [build] command = "npm run build" functions = "netlify/functions"
[functions] node_bundler = "esbuild" # Env vars are now accessible at runtime ```
Then update your function:
```javascript // netlify/functions/payment.js exports.handler = async (event) => { const stripeKey = process.env.STRIPE_SECRET_KEY; console.log(stripeKey); // logs: sk_live_xxxxx return { statusCode: 200, body: JSON.stringify({ key: stripeKey }) }; }; ```
Approach 2: Force redeploy after setting env vars
1. Add env var in Netlify UI (Site settings → Build & deploy → Environment)
2. Trigger redeploy: netlify deploy --prod
3. New build captures env vars at runtime
Approach 3: Use .env.example + manual configuration
```bash
.env.example (commit to repo)
STRIPE_SECRET_KEY=your_key_here DATABASE_URL=your_db_url ```In Netlify UI:
.env.exampleprocess.env.VARIABLE_NAME---
Why This Happens
Netlify Functions run in AWS Lambda. Environment variables set in the Netlify dashboard are injected at build time for build scripts, but functions need explicit configuration or a fresh deploy to capture them at runtime. This is different from traditional Node.js servers where env vars load on startup.
Version note: This behavior is consistent across 2024-2026 Netlify deployments, though exact injection timing can vary with different Node.js runtimes (18.x vs 20.x). If you're unsure which runtime you're using, check Logs in Netlify dashboard.
---
Still broken? Check these too
1. Env var name typo in function code — Netlify won't warn you if you reference STRIPE_SECRET but defined STRIPE_SECRET_KEY. Add a startup validation:
```javascript
if (!process.env.STRIPE_SECRET_KEY) {
throw new Error('Missing STRIPE_SECRET_KEY');
}
```
2. Using require('dotenv').config() in functions — Netlify Functions can't read .env files from repo at runtime. Remove dotenv calls; rely on Netlify UI env vars instead. See [environment variables guide](/?guide=netlify-env-setup).
3. Cached Lambda layer — If you recently changed env vars, AWS may serve old function code. Force clear: Go to Netlify UI → Deploys → trigger a full redeploy, don't use git push alone. This issue sometimes appears as "env vars worked yesterday" at 2am on production.
---
Official Resources
For related issues, also see [debugging Netlify function timeouts](/?guide=netlify-timeout-fix) and [build logs not showing errors](/?guide=netlify-build-debugging).
---
Found a different variation? Drop it in the comments — If you hit this with monorepos, esm imports, or specific frameworks like Next.js/Remix, share your exact error and fix. We'll add it here.