Netlify: env vars not loading in functions [2026 fix]
Env vars undefined in Netlify Functions? Deploy without committing netlify.toml or use process.env inside function handler, not at module scope.
Netlify: env vars not loading in functions [2026 fix]
TL;DR
Cause: Environment variables are evaluated at build time or outside function scope, but Netlify Functions load them at runtime inside the handler.
Fix: Move process.env access inside your function handler and ensure netlify.toml exists in repo without sensitive values, or use Netlify UI environment variables exclusively.
---
Real Console Error Messages
``` 1. TypeError: Cannot read property 'API_KEY' of undefined at Object.<module> (/var/task/src/functions/api.js:5:3)
2. ReferenceError: process is not defined at /var/task/src/functions/fetch-data.js:1:1
3. Error: Missing required environment variable DATABASE_URL at connectDB (/var/task/src/functions/db.js:12:5)
4. TypeError: Cannot read properties of undefined (reading 'split') at /var/task/src/functions/auth.js:7:18
5. SyntaxError: Unexpected token } in JSON at position 0 when parsing process.env.CONFIG as JSON at module load time ```
---
Broken Code vs. Exact Fix
❌ BROKEN: Module-scope env access
```javascript // netlify/functions/api.js - WRONG const API_KEY = process.env.API_KEY; // ← undefined at build time const API_URL = process.env.API_URL; // ← evaluated once at deploy
exports.handler = async (event) => {
try {
const response = await fetch(${API_URL}/data, {
headers: { 'Authorization': Bearer ${API_KEY} }
});
return { statusCode: 200, body: JSON.stringify(response) };
} catch (error) {
return { statusCode: 500, body: error.message };
}
};
```
✅ FIXED: Handler-scope env access
```javascript
// netlify/functions/api.js - CORRECT
exports.handler = async (event) => {
const API_KEY = process.env.API_KEY; // ← accessed at runtime
const API_URL = process.env.API_URL; // ← fresh each invocation
if (!API_KEY || !API_URL) {
return {
statusCode: 500,
body: JSON.stringify({ error: 'Missing env vars' })
};
}
try {
const response = await fetch(${API_URL}/data, {
headers: { 'Authorization': Bearer ${API_KEY} }
});
return { statusCode: 200, body: JSON.stringify(response) };
} catch (error) {
return { statusCode: 500, body: error.message };
}
};
```
❌ BROKEN: Netlify.toml with secrets
```toml
netlify.toml - WRONG (in git repo)
[build] command = "npm run build" functions = "netlify/functions"[build.environment] API_KEY = "sk_live_abc123xyz" # ← EXPOSED IN GIT DATABASE_PASSWORD = "secret123" ```
✅ FIXED: Use UI environment variables only
```toml
netlify.toml - CORRECT (safe to commit)
[build] command = "npm run build" functions = "netlify/functions"No secrets here. Set via Netlify UI:
Site Settings → Build & Deploy → Environment
```Then in Netlify Dashboard:
API_KEY, API_URL, DATABASE_PASSWORD---
Step-by-Step Fix
1. Audit your code: Search for process.env at module scope (top-level, outside functions)
2. Move inside handler: Wrap all process.env reads inside exports.handler
3. Check netlify.toml: Remove any sensitive values; use only build config
4. Set via UI: Netlify Dashboard → Site Settings → Environment → Add variables
5. Test locally: Use .env.example (no secrets) and netlify dev with --env flag
6. Redeploy: Full rebuild triggers fresh env var injection
I'm uncertain about: Whether environment variable timing changed between Netlify Functions v1 and the latest 2026 runtime versions. Test thoroughly in staging if deploying to production.
---
Still broken? Check these too
1. Variables not in "Deployed context": Netlify UI has separate env sections (Production, Deploy Preview, Branch Deploy). Check the correct context matches your build. 2. Netlify Functions vs. Edge Functions: Edge Functions (Deno) require different env syntax. See [Edge Functions environment variables](/docs-link-edge) vs. regular Functions. 3. Race condition in concurrent functions: If multiple functions read the same var simultaneously during cold start, timing issues may occur. Add exponential backoff or lazy-load env vars.
---
Related guides
---
Official Documentation
[Netlify Functions Environment Variables](https://docs.netlify.com/functions/overview/#environment-variables)
---
Found a different variation? Drop it in the comments.