Netlify: env vars not loading in functions [2026 fix]
Env vars undefined in Netlify Functions? Deploy netlify.toml with [functions] config + restart build. One-line fix inside.
Netlify: env vars not loading in functions [2026 fix]
TL;DR
Cause: Netlify Functions can't access env vars becausenetlify.toml lacks [functions] configuration or build didn't redeploy after env var addition.
Fix: Add [functions] block to netlify.toml, set env vars in Netlify dashboard, then trigger a new deploy.---
Exact Console Error Messages
Here are real errors you'll see at 2am:
``` 1. TypeError: Cannot read property of undefined at Runtime.handler (/var/task/functions/api.js:15:23)
2. Error: process.env.DATABASE_URL is undefined at Object.<anonymous> (/var/task/functions/db.js:5:10)
3. ReferenceError: STRIPE_KEY is not defined at module.exports (/var/task/functions/payment.js:8:5)
4. SyntaxError: Unexpected token u in JSON at position 0 (happens when function tries to use undefined env var as JSON)
5. 502 Bad Gateway Function crashed because env var was null during initialization ```
---
Broken Code → Fixed Code (Side by Side)
BROKEN: Missing netlify.toml config
```toml
netlify.toml (incomplete)
[build] command = "npm run build" publish = "dist" ```FIXED: Add [functions] block
```toml
netlify.toml (correct)
[build] command = "npm run build" publish = "dist"[functions] directory = "netlify/functions" node_bundler = "esbuild" ```
---
BROKEN: Function accessing env var without config
```javascript // netlify/functions/api.js exports.handler = async (event) => { const dbUrl = process.env.DATABASE_URL; const client = new Database(dbUrl); // undefined here! return { statusCode: 200 }; }; ```
FIXED: Ensure netlify.toml + redeploy
```javascript // netlify/functions/api.js (same code, but now env vars load) exports.handler = async (event) => { const dbUrl = process.env.DATABASE_URL; if (!dbUrl) { return { statusCode: 500, body: JSON.stringify({ error: "DATABASE_URL not set" }) }; } const client = new Database(dbUrl); // NOW defined! return { statusCode: 200 }; }; ```
Action:
1. Add [functions] block to netlify.toml
2. Go to Netlify Dashboard → Site Settings → Build & Deploy → Environment
3. Add env vars (e.g., DATABASE_URL=postgres://...)
4. Trigger new deploy: git push or click "Trigger deploy"
5. Wait 2-3 minutes for build to complete
---
Why This Happens
Netlify Functions are serverless (AWS Lambda under the hood). Unlike browser-side code, functions require explicit configuration in netlify.toml to bundle and load environment variables at runtime. Without the [functions] block:
process.env returns undefined even if vars are set in dashboardVersion note: This behavior is consistent across Netlify CLI v11+ and Netlify platform (2024-2026). If you're using legacy netlify-lambda (pre-2020), different setup required—specify in comments if applicable.
---
Still broken? Check these too
1. Wrong directory path: Confirm functions are in netlify/functions/ (not functions/ or src/functions/). Netlify's default expects netlify/functions/.
2. Env vars not saved: Click the env var input field, hit Enter, then verify it appears in the list. Some users paste values but forget to confirm. Check dashboard again after 30 seconds—vars should persist.
3. Local dev mismatch: Running locally with .env file works differently. Install netlify-cli (npm i -g netlify-cli) and run netlify dev instead of npm start. This respects netlify.toml and dashboard env vars. See [local env vars guide](/?guide=netlify-local-dev).
4. Function cold start timeout: If function still crashes, add timeout handling and verify node_bundler matches your code (esbuild for modern JS, default for Node 18 CommonJS). See [function performance](/?guide=netlify-function-perf).
---
Checklist Before Escalating
netlify.toml has [functions] section?git push to trigger deploy (or clicked "Trigger deploy")?netlify dev locally, not npm start?---
Official docs: [Netlify Functions environment variables](https://docs.netlify.com/functions/overview/#environment-variables)
---
Found a different variation? Drop it in the comments—share your error message and what fixed it for you.