Netlify: env vars not loading in functions [2026 fix]
Environment variables undefined in Netlify Functions due to missing netlify.toml config or incorrect build settings.
Netlify: env vars not loading in functions [2026 fix]
TL;DR
Cause: Your netlify.toml lacks a [functions] section or environment variables aren't declared in Netlify UI/toml, causing functions to receive undefined instead of values.
Fix: Add [functions] block to netlify.toml and redeploy, OR set env vars in Netlify dashboard → Site settings → Build & deploy → Environment.
---
Real Console Error Messages
You'll see one or more of these:
``` TypeError: Cannot read property 'toLowerCase' of undefined at handler (/var/task/function.js:15:3) ```
``` ReferenceError: process.env.STRIPE_KEY is not defined at exports.handler (/opt/nodejs/node_modules/handler.js:8:12) ```
``` Error: Missing required environment variable: DB_URL undefined !== 'postgresql://...' ```
``` Function invocation failed: Error processing event with Lambda runtime Environment variable "API_TOKEN" returned undefined ```
``` Netlify Function error: process.env.SUPABASE_URL is undefined Check your netlify.toml [functions] config or dashboard settings ```
---
Broken Code vs. Exact Fix
❌ BROKEN: Missing netlify.toml configuration
```toml
netlify.toml (incomplete)
[build] command = "npm run build" publish = "dist"← No [functions] section!
``````javascript // functions/api.js exports.handler = async (event) => { const apiKey = process.env.STRIPE_KEY; // undefined! console.log(apiKey); // logs: undefined return { statusCode: 500, body: "Missing API key" }; }; ```
✅ FIXED: Add [functions] block + redeploy
```toml
netlify.toml (complete)
[build] command = "npm run build" publish = "dist"[functions] node_bundler = "esbuild" directory = "functions" ```
```javascript // functions/api.js (unchanged code now works) exports.handler = async (event) => { const apiKey = process.env.STRIPE_KEY; // Now loads correctly! console.log(apiKey); // logs: sk_live_... return { statusCode: 200, body: "Success" }; }; ```
Also add via Netlify Dashboard:
1. Go to Site settings → Build & deploy → Environment
2. Click Edit variables
3. Add key-value pairs:
- STRIPE_KEY = sk_live_...
- DB_URL = postgresql://...
4. Deploy site (Settings → Deploys → trigger deploy)
---
Alternative: Using .env.example + .env locally
If you want local testing to match production:
```bash
.env.example (commit to git)
STRIPE_KEY= DB_URL= API_TOKEN=.env.local (gitignored, local only)
STRIPE_KEY=sk_test_local DB_URL=postgresql://localhost API_TOKEN=dev_token_123 ``````javascript // functions/api.js const stripe = require('stripe')(process.env.STRIPE_KEY); // Reads from .env.local locally, from dashboard vars on Netlify ```
---
Version-specific behavior
I'm uncertain whether: Netlify Functions behavior differs between Node 14, 16, 18, and 20 runtimes regarding env var timing. If you're on an older runtime, try explicitly specifying runtime in netlify.toml:
```toml [functions] node_bundler = "esbuild" node_version = "18.0.0" ```
Older versions may require full redeployment (not just pushing new code) for env vars to take effect.
---
Still broken? Check these too
1. Build-time vs. Runtime variables: If your variables are used during npm run build, set them in dashboard *and* in Build & deploy → Environment. Frontend build vars need NETLIFY_ prefix in some cases—[see our guide on build vars](/?guide=netlify-build-env).
2. Netlify CLI local testing: Running netlify dev locally pulls env vars from .env files, not the dashboard. Use netlify env:list to verify what's deployed, then netlify env:set KEY value to push new vars without UI.
3. Function timeout or cold start issues: Environment variables *are* loaded, but function fails before using them. Check CloudWatch logs in Netlify → Functions tab. Also see [our Lambda timeout guide](/?guide=netlify-function-timeout).
---
Official Documentation
---
Quick Checklist
netlify.toml has [functions] section with directory = "functions"process.env.VARIABLE_NAME in function, not process.env['VARIABLE_NAME'] (both work, but verify syntax)netlify/functions/ or custom in netlify.toml)---
Found a different variation? Drop it in the comments.