Netlify: env vars not loading in functions [2026 fix]
Netlify Functions can't access env vars because they're defined in UI but not deployed to function runtime. Solution: add vars to netlify.toml [functions] context.
Netlify: env vars not loading in functions [2026 fix]
TL;DR
Cause: Environment variables defined in Netlify UI don't automatically inject into Functions runtime context without explicit configuration. Fix: Define env vars innetlify.toml under [functions] or redeploy after UI changes with cache cleared.---
Real Console Error Messages
``` 1. undefined is not an object (evaluating 'process.env.API_KEY') at Object.<anonymous> (.netlify/functions/api:1:200)
2. Error: process.env.DATABASE_URL is undefined at connectDB (/var/task/functions/db.js:5:15) at Runtime.handler (/var/task/functions/db.js:12:3)
3. TypeError: Cannot read property 'STRIPE_SECRET' of undefined at module.exports (.netlify/functions/payment.js:3:8)
4. ConfigError: Missing required env var: JWT_SECRET for function 'auth'
5. ReferenceError: process is not defined (when using Node 18+ runtime without proper bundling) ```
---
Broken Code vs Fixed Code
Issue 1: Env vars only in Netlify UI
❌ BROKEN:
```javascript
// .netlify/functions/api.js
exports.handler = async (event) => {
const apiKey = process.env.API_KEY; // undefined!
const response = await fetch('https://api.example.com', {
headers: { Authorization: Bearer ${apiKey} }
});
return { statusCode: 200, body: JSON.stringify(response) };
};
```
✅ FIXED: ```toml
netlify.toml
[functions] environment = { API_KEY = "your-key-here" } ```OR for development + production: ```toml [functions] environment = { API_KEY = "$API_KEY" }
[context.production] [context.production.functions] environment = { API_KEY = "$PROD_API_KEY" } ```
Then keep secrets in Netlify UI (Site settings → Build & deploy → Environment).
---
Issue 2: Missing redeploy after adding env vars in UI
❌ BROKEN:
1. Add API_KEY in Netlify UI
2. Redeploy only via git push
3. process.env.API_KEY still undefined
✅ FIXED: ```bash
Option A: Trigger rebuild via Netlify UI
Go to: Deploys tab → Trigger deploy → Deploy site
Option B: Clear cache and redeploy
Deploys → Deploy settings → Clear cache and deploy site
Option C: Use Netlify CLI
npm install -g netlify-cli netlify env:set API_KEY "your-actual-key" netlify deploy --prod --build ```---
Issue 3: Functions context mismatch
❌ BROKEN: ```javascript // Tries to read vars before function handler const API_KEY = process.env.API_KEY;
exports.handler = async (event) => { // API_KEY is undefined here return { statusCode: 200, body: API_KEY }; }; ```
✅ FIXED: ```javascript // Read inside handler function exports.handler = async (event) => { const API_KEY = process.env.API_KEY; // Now defined! return { statusCode: 200, body: JSON.stringify({ key: API_KEY }) }; }; ```
---
Why This Happens
Netlify Functions run in an isolated Lambda-like environment. Environment variables set in the Netlify UI are stored separately from your function code and only injected at runtime. If you:
.env (local dev only)netlify.toml setupThe functions won't see them.
---
Still broken? Check these too
1. Node version mismatch: If using Node 18+ and getting ReferenceError: process is not defined, your function may have incorrect bundling. Verify runtime = "nodejs18.x" in netlify.toml and rebuild.
2. Local vs production discrepancy: .env files work locally but not in production. Always test with netlify functions:invoke or check actual deploy logs (Site settings → Deploys → View deploy log).
3. Stale cache in browser: Functions cached a bad result. Clear your browser cache, or add Cache-Control: no-cache header and wait 10 minutes for edge cache to expire. Also see [Netlify cache troubleshooting guide](/?guide=cache-busting).
---
Verification Checklist
```javascript // Add this to your function to debug exports.handler = async (event) => { console.log('Available env vars:', Object.keys(process.env).filter(k => k.includes('API'))); console.log('API_KEY value:', process.env.API_KEY ? '[SET]' : '[MISSING]'); return { statusCode: 200 }; }; // Check in: Netlify UI → Functions → Logs (or CloudWatch) ```
---
Related Issues
---
Official Docs
[Netlify Environment Variables Documentation](https://docs.netlify.com/configure-builds/environment-variables/)
---
Found a different variation? Drop it in the comments—env var issues have many flavors at 2am.