Netlify: env vars not loading in functions [2026 fix]
Netlify Functions can't access env vars because they're defined in UI but not deployed to function runtime. Redeploy after setting vars or use netlify.toml.
TL;DR
Cause: Environment variables set in Netlify UI aren't automatically injected into Function runtime without a fresh deploy.
Fix: Either redeploy your site after adding env vars, or define them in netlify.toml with [functions] context.
---
Real Error Messages You'll See
``` ReferenceError: process is not defined at Object.<anonymous> (/var/task/index.js:5:23) ```
``` TypeError: Cannot read property 'MY_API_KEY' of undefined at handler (/var/task/functions/api.js:12:8) ```
``` Error: process.env.DATABASE_URL is undefined at connectDB (file:///var/task/index.mjs:3:15) ```
``` {"errorMessage":"Cannot destructure property 'API_TOKEN' of 'process.env' as it is undefined."} ```
``` netlify functions:invoke myFunction -- --no-identity Error: Environment variable STRIPE_KEY not found ```
---
Broken Code vs. Fixed Code
❌ Broken: Relying only on UI vars without redeploy
```javascript // functions/charge.js exports.handler = async (event) => { const stripeKey = process.env.STRIPE_SECRET_KEY; // You added STRIPE_SECRET_KEY in Netlify UI // but never triggered a redeploy if (!stripeKey) { return { statusCode: 500, body: 'Key missing' }; } return { statusCode: 200, body: stripeKey }; }; ```
✅ Fixed: Define in netlify.toml + redeploy
```toml
netlify.toml
[functions] node_bundler = "esbuild"[[context.production.env]] name = "STRIPE_SECRET_KEY" value = "sk_live_xxx"
[[context.development.env]] name = "STRIPE_SECRET_KEY" value = "sk_test_yyy" ```
```javascript // functions/charge.js (same code, now works) exports.handler = async (event) => { const stripeKey = process.env.STRIPE_SECRET_KEY; if (!stripeKey) { return { statusCode: 500, body: 'Key missing' }; } return { statusCode: 200, body: stripeKey }; }; ```
Then redeploy: ```bash git add netlify.toml git commit -m "Add env vars to toml" git push origin main ```
Alternative: UI-only vars (requires redeploy)
1. Go to Site settings → Build & deploy → Environment
2. Add variable: STRIPE_SECRET_KEY = sk_live_xxx
3. Click Trigger deploy (don't just save)
4. Wait for build to complete
5. Test function—vars now available
---
Why This Happens
Netlify Functions run in isolated Lambda-like containers. Environment variables must be baked into the deployment bundle OR passed at invoke time. Setting them in the UI creates them for your *build process* but older deployment bundles won't have them. Functions already deployed before the env var was added won't see the new variables until you redeploy.
I'm uncertain whether this behavior changed between Netlify CLI v17 vs v18+. Test locally with netlify functions:invoke to verify your setup before pushing.
---
Still Broken? Check These Too
1. Local vs. Production Mismatch
Problem: Works withnetlify dev but not in production.
Solution:
production vs development)netlify functions:invoke --context=production to test production env vars locally2. Function Not Being Deployed
Problem: Function file exists locally but missing from deployment. Solution:netlify/functions/ folder (or folder specified in netlify.toml [build] functions field)netlify deploy --prod --dry-run to see what's being deployed.netlifyignore excluding your functions folder3. ESM vs CommonJS Module Format
Problem:process.env works in CommonJS but not ES modules.
Solution:
const key = process.env.MY_VAR ✅import { env } from 'process'; const key = env.MY_VAR or use process.env directly (Netlify supports both)node_bundler setting in netlify.toml matches your code style---
Official Resources
---
Related Guides
---
Action Checklist for 2am
netlify.toml exist? No → Create it with [functions] contextnetlify env:list locally—see your vars? No → Re-authenticate with netlify login.js or .mjs? Confirm bundler setting---
Found a different variation? Drop it in the comments.