Netlify: env vars not loading in functions [2026 fix]
Env vars undefined in Netlify Functions? Deploy netlify.toml with [functions] config or use process.env after build step.
Netlify: env vars not loading in functions [2026 fix]
TL;DR
Cause: Environment variables aren't being passed to your serverless functions becausenetlify.toml lacks proper function configuration or vars are set in UI but not bundled at deploy time.
Fix: Add [functions] config block to netlify.toml or ensure vars are set *before* deploy, not injected post-deployment.---
Real Console Error Messages
Here are exact error patterns you'll see at 2am:
``` TypeError: Cannot read property 'API_KEY' of undefined at Handler (/var/task/index.js:1:500) at Runtime.handler (internal/index.js:32:15) ```
``` ReferenceError: process is not defined at Object.<anonymous> (/var/task/function.js:12:3) ```
``` Error: STRIPE_SECRET_KEY is undefined. Check your environment variables. Function invoked without required .env context. ```
``` Netlify Functions runtime error: Missing environment variable 'DATABASE_URL' Available vars: NODE_ENV, NETLIFY ```
``` 502 Bad Gateway - Function failed to initialize Error: process.env.AUTH_TOKEN returned undefined during cold start ```
---
Broken vs. Fixed Code
Problem 1: Missing netlify.toml Configuration
BROKEN: ```toml
netlify.toml (incomplete)
[build] command = "npm run build" publish = "dist" ```FIXED: ```toml
netlify.toml (correct)
[build] command = "npm run build" publish = "dist"[functions] directory = "netlify/functions" node_bundler = "esbuild" ```
Problem 2: Accessing env vars before assignment
BROKEN: ```javascript // netlify/functions/api.js const apiKey = process.env.API_KEY; // undefined at parse time
exports.handler = async (event) => { return { statusCode: 200, body: JSON.stringify({ key: apiKey }) }; }; ```
FIXED: ```javascript // netlify/functions/api.js exports.handler = async (event) => { const apiKey = process.env.API_KEY; // read at runtime if (!apiKey) { return { statusCode: 500, body: JSON.stringify({ error: 'API_KEY not configured' }) }; } return { statusCode: 200, body: JSON.stringify({ key: apiKey }) }; }; ```
Problem 3: UI vars not synced with Build & Deploy
BROKEN: ```bash
You manually add to Netlify UI > Site settings > Environment
But deploy happens before vars load
Sequence: Deploy starts → Functions bundle → UI vars ignored → Deploy completes
```FIXED: ```bash
Step 1: Set vars in Netlify UI (Site settings > Environment variables)
Step 2: Trigger REDEPLOY (not just push code)
Netlify rebuilds functions with vars in scope
OR use netlify.toml with .env.example:
echo "API_KEY=your_key_here" > .env.productionThen commit: Netlify reads during build
```---
Why This Happens
Netlify Functions are bundled at deploy time, not runtime. If you:
1. Set env vars in the UI *after* deploying code → Functions already bundled without them
2. Forget [functions] config → Functions directory not recognized
3. Access process.env at module level instead of inside handler → Evaluated before vars exist
The fix ensures vars exist before the build step bundles your functions.
---
Still broken? Check these too
1. Env var name mismatch: Verify exact spelling. API_KEY ≠ api_key ≠ API_key. Check Netlify UI for typos.
2. Context mismatch (production vs preview): You set vars for production branch only. Redeploy your preview branch or set vars for all contexts in Site settings > Environment > Advanced.
3. Cached function bundle: Old code deployed despite UI changes. Fix: Go to Netlify UI → Deploys → Trigger deploy → Clear cache and redeploy.
---
Verification Checklist
```javascript // Quick test inside your function handler: exports.handler = async (event) => { console.log('All env vars:', Object.keys(process.env)); console.log('API_KEY exists?', !!process.env.API_KEY); console.log('API_KEY value:', process.env.API_KEY?.substring(0, 5) + '***'); // ... rest of function }; ```
Deploy, invoke the function, check logs. If your var name doesn't appear in the list, it's not loaded.
---
Version Notes
This guide applies to Netlify as of 2026. Netlify Functions runtime may change how env vars are injected. If you're on an older version (pre-2023), check official docs below—bundling behavior differed slightly.
---
Related Issues
---
Official Resources
---
Found a different variation? Drop it in the comments—we update this guide based on real 2am issues our community hits.