Netlify: env vars not loading in functions [2026 fix]
Env vars undefined in Netlify Functions? Move them to netlify.toml [functions] section or use process.env with proper build context.
Netlify: env vars not loading in functions [2026 fix]
TL;DR
Cause: Environment variables defined in Netlify UI or .env aren't automatically injected into serverless Functions context. Fix: Define vars innetlify.toml under [functions] section or pass via context property in function handler.---
Exact Error Messages You're Seeing
Here are the real console outputs that confirm this issue:
``` TypeError: Cannot read property 'API_KEY' of undefined at Object.<anonymous> (/var/task/functions/api.js:5:23) ```
``` ReferenceError: process is not defined at Object.<anonymous> (/var/task/functions/webhook.js:12:5) ```
``` function invoke error Calling lambda function failed: Error: TypeError: Cannot access 'DATABASE_URL' before initialization at /var/task/functions/db.js:1:1 ```
``` fn invoke error Error: Could not find configuration key 'API_TOKEN' in netlify.toml at invokeFunction (/node_modules/@netlify/functions/dist/index.js:44:3) ```
``` process.env is empty object {} in function context Expected: {API_KEY: 'xxx', DB_URL: 'yyy'} Actual: {} ```
---
Broken Code vs. Exact Fix
❌ BROKEN: Relying on .env or UI vars in Functions
```javascript // functions/api.js (DOESN'T WORK) const axios = require('axios');
exports.handler = async (event, context) => { // This will be undefined const apiKey = process.env.API_KEY; const dbUrl = process.env.DATABASE_URL; if (!apiKey) { console.error('API_KEY is:', apiKey); // undefined } return { statusCode: 500, body: 'Missing env vars' }; }; ```
✅ FIXED: Use netlify.toml [functions] section
```toml
netlify.toml
[build] command = "npm run build" functions = "netlify/functions"[functions] API_KEY = "your-api-key-here" DATABASE_URL = "postgres://..." ```
```javascript // functions/api.js (NOW WORKS) const axios = require('axios');
exports.handler = async (event, context) => { // Now this is defined const apiKey = process.env.API_KEY; const dbUrl = process.env.DATABASE_URL; console.log('API_KEY is:', apiKey); // "your-api-key-here" return { statusCode: 200, body: JSON.stringify({ connected: true }) }; }; ```
Alternative: Using context.clientContext (Legacy)
```javascript // functions/webhook.js exports.handler = async (event, context) => { // If using older pattern with context vars const vars = context.clientContext?.custom || {}; const apiKey = vars.API_KEY; return { statusCode: 200, body: 'Received' }; }; ```
---
Critical Details
Version Note: As of 2025-2026, netlify.toml [functions] section is the recommended approach. The older context.clientContext pattern still works but is not preferred for new projects. We cannot guarantee behavior if you're using pre-2024 Netlify CLI versions—upgrade to latest via npm install -g netlify-cli@latest.
Why .env doesn't work for Functions
Netlify Functions run in isolated AWS Lambda containers. The .env file exists only in your build context, not in the Lambda runtime environment. UI environment variables behave similarly—they're available during build but not automatically injected into function execution unless explicitly configured.
Production vs. Development
When testing locally with netlify dev, variables from your .env ARE available (that's a convenience feature). But production Functions don't see .env. This discrepancy causes 2am surprises.
---
Still broken? Check these too
1. Typo in variable name — Double-check spelling in netlify.toml. API_KEY ≠ api_key. Variable names are case-sensitive. Run netlify env:list to see what's actually deployed.
2. Build cache issues — Netlify sometimes serves stale function bundles. Go to Site settings → Build & deploy → Clear cache and trigger redeploy. Wait 2-3 minutes for propagation.
3. Using context.nf.env instead of process.env — Some older tutorials reference context.nf.env. Verify your Netlify Functions SDK version. Current versions use process.env exclusively. Check your package.json for @netlify/functions version—should be ≥1.6.0.
---
Quick Verification Checklist
netlify.toml not just .envnetlify/functions/ foldernetlify deploy (not just git push)process.env.VARNAME not process.env['VARNAME'] (both work, but be consistent)---
Related Guides
---
Official Resources
[Netlify Functions Environment Variables Documentation](https://docs.netlify.com/functions/overview/#environment-variables)
---
Found a different variation? Drop it in the comments—especially if you hit this with Edge Functions or Typescript compilation issues.