Netlify: env vars not loading in functions [2026 fix]
Functions can't access env vars because they're not deployed with the build context or require explicit netlify.toml configuration.
Netlify: env vars not loading in functions [2026 fix]
TL;DR
Cause: Environment variables defined in Netlify UI aren't automatically passed to serverless functions unless explicitly configured in netlify.toml.
Fix: Add environment context to your netlify.toml or redeploy after setting vars in UI (clearing cache if needed).
---
Exact Error Messages You'll See
``` undefined is not a function TypeError: Cannot read property 'API_KEY' of undefined ```
``` ReferenceError: process.env.DATABASE_URL is not defined ```
``` [Functions] Environment variable "STRIPE_SECRET" not available in function context ```
``` Error: Missing required environment variable: AUTH_TOKEN at runtime ```
``` Warning: Environment variables not injected into serverless functions during build ```
---
Broken Code vs. Fixed Code
❌ Broken: netlify/functions/api.js
```javascript // Functions expecting env vars without netlify.toml config exports.handler = async (event) => { const apiKey = process.env.API_KEY; // Returns undefined const dbUrl = process.env.DATABASE_URL; // Returns undefined return { statusCode: 200, body: JSON.stringify({ key: apiKey, db: dbUrl }) }; }; ```
✅ Fixed: netlify.toml + netlify/functions/api.js
netlify.toml: ```toml [functions] node_bundler = "esbuild" # Explicitly include environment variables for functions environment = ["API_KEY", "DATABASE_URL", "STRIPE_SECRET"]
Alternative: expose all vars (less secure, use cautiously)
[context.production.environment]
API_KEY = "$API_KEY"
DATABASE_URL = "$DATABASE_URL"
```netlify/functions/api.js: ```javascript // Now env vars load correctly exports.handler = async (event) => { const apiKey = process.env.API_KEY; // ✓ Works const dbUrl = process.env.DATABASE_URL; // ✓ Works if (!apiKey || !dbUrl) { return { statusCode: 500, body: JSON.stringify({ error: "Missing env vars" }) }; } return { statusCode: 200, body: JSON.stringify({ key: apiKey, db: dbUrl }) }; }; ```
---
Why This Happens
Netlify's environment variables set in the UI apply to your build process and site context, but serverless functions run in isolated Lambda containers. They need explicit configuration to inherit those variables. This is a deliberate security boundary — not a bug.
Version note: Behavior consistent across Netlify as of 2026. Confirm your netlify-cli is updated: npm install -g netlify-cli@latest.
---
Step-by-Step Fix
1. Verify vars exist in Netlify UI
- Site settings → Build & deploy → Environment
- Confirm your API_KEY, DATABASE_URL, etc. are listed
2. Update netlify.toml (in root directory) ```toml [functions] environment = ["API_KEY", "DATABASE_URL"] ```
3. Redeploy ```bash git add netlify.toml git commit -m "Fix: expose env vars to functions" git push ```
4. Clear Netlify cache (if still broken) - Site settings → Deploys → Trigger deploy → Clear cache and deploy
5. Test locally with .env.example ```bash # Create .env.local for local testing echo 'API_KEY=test_key' > .env.local netlify dev # Runs with env vars ```
---
Still broken? Check these too
1. Function isn't in netlify/functions/ directory
Netlify only deploys functions from the correct directory. Verify structure:
```
project/
├─�� netlify.toml
├── netlify/
│ └── functions/
│ └── api.js ✓ Correct
└── .netlifyignore
```2. Variable name typo between UI and code
Environment variables are case-sensitive.API_KEY ≠ api_key. Cross-check letter-for-letter in Netlify UI vs. your code.3. Using old Netlify Functions syntax
If you're on an old project, ensure you're using ES6 exports: ```javascript // ✓ Modern (works) exports.handler = async (event) => { }// ✗ Old (may not load env vars correctly) module.exports.handler = function(event, context) { } ```
---
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
If you hit a *different* error with env vars (e.g., in edge functions, background functions, or specific frameworks like Next.js), reply below. We'll add it to this guide.