Netlify: env vars not loading in functions [2026 fix]

Environment variables undefined in Netlify Functions because netlify.toml missing or functions dir misconfigured. Fix: add [[functions]] config block.

Netlify: env vars not loading in functions [2026 fix]

TL;DR

Cause: Your netlify.toml is missing the [[functions]] configuration block or your environment variables aren't being passed to the functions runtime.

Fix: Add `[[functions]]] directory = "netlify/functions" to netlify.toml` and redeploy—Netlify won't inject env vars into functions without explicit function configuration.

---

Common Error Messages

You'll see one or more of these in your function logs at 2am:

``` TypeError: Cannot read property 'API_KEY' of undefined at Runtime.getUserData ReferenceError: process.env.STRIPE_SECRET_KEY is not defined Error: Environment variable DATABASE_URL returned undefined in production Netlify function invoked but process.env is empty object {} FunctionError: handler returned undefined; env vars missing from context ```

---

The Problem: Broken vs Fixed Code

❌ Broken Setup

netlify.toml (missing or incomplete): ```toml [build] command = "npm run build" publish = "dist"

NO [[functions]] block = env vars don't load!

```

netlify/functions/api.js: ```javascript exports.handler = async (event, context) => { const apiKey = process.env.API_KEY; // undefined! const dbUrl = process.env.DATABASE_URL; // undefined! return { statusCode: 500, body: JSON.stringify({ error: "Missing credentials" }) }; }; ```

✅ Fixed Setup

netlify.toml (correct configuration): ```toml [build] command = "npm run build" publish = "dist"

[[functions]] directory = "netlify/functions" node_bundler = "esbuild" ```

netlify/functions/api.js (same code, now works): ```javascript exports.handler = async (event, context) => { const apiKey = process.env.API_KEY; // now loaded! const dbUrl = process.env.DATABASE_URL; // now loaded! return { statusCode: 200, body: JSON.stringify({ success: true, key: apiKey.substring(0,4) }) }; }; ```

Critical difference: The [[functions]] block tells Netlify to parse your function files and inject environment variables.

---

Step-by-Step Fix

1. Check if netlify.toml exists in your repo root: ```bash ls -la netlify.toml ``` If missing, create it.

2. Add the functions configuration block: ```toml [[functions]] directory = "netlify/functions" node_bundler = "esbuild" ``` Note: node_bundler = "esbuild" is default in 2026; if you're on an older version, you may need node_bundler = "zisi" instead. Check your Netlify UI deploy logs to confirm.

3. Verify environment variables are set in Netlify UI: - Site settings → Build & deploy → Environment - Confirm API_KEY, DATABASE_URL, etc. are present - Variables set here are automatically available to functions

4. Redeploy: ```bash git add netlify.toml git commit -m "fix: add functions config to netlify.toml" git push ```

5. Check function logs (Netlify UI → Functions → click function name → logs tab)

---

Still Broken? Check These Too

1. Wrong directory path: If your functions are in functions/ not netlify/functions/, update the directory value in [[functions]] to match. Netlify won't find functions if the path is wrong.

2. Environment variable name typos: Netlify is case-sensitive. API_KEY ≠ api_key. Check your Site settings match your code exactly. Use console.log(Object.keys(process.env)) in your function to debug what's actually available.

3. Stale build cache: If you've recently changed netlify.toml, clear Netlify's build cache (Site settings → Build & deploy → Deploy contexts → Clear cache and redeploy). Old deployments may have cached the broken config.

---

Uncertainty Note

We're explicit here: Node bundler behavior changed between 2024-2026. If your deploy logs show zisi or nft bundler, that's an older config. Modern Netlify defaults to esbuild, which handles env vars more reliably. If adding node_bundler = "esbuild" causes issues, try removing that line and letting Netlify auto-detect.

---

Related Troubleshooting

  • [Netlify Functions 404 errors: directory misconfiguration](/guide=netlify-404-functions)
  • [CORS headers not working in Netlify Functions](/guide=netlify-cors-headers)
  • ---

    Official Resources

  • [Netlify Functions environment variables (official docs)](https://docs.netlify.com/functions/overview/#environment-variables)
  • [netlify.toml configuration reference](https://docs.netlify.com/configure-builds/file-api-reference/)
  • ---

    Found a different variation? Drop it in the comments

    If you hit a different error or env var scenario (e.g., loading from .env.local vs Netlify UI, monorepo setups, TypeScript functions), please comment below—we'll update this guide with your solution.

    🔥 0d
    LIVE
    ↓ PlanetScale rage spiking •⚠ Vercel pricing complaints •↑ Railway gaining fast •↑ Supabase happiness rising •↑ Resend loved by devs •↓ PlanetScale rage spiking •⚠ Vercel pricing complaints •↑ Railway gaining fast •↑ Supabase happiness rising
    DEVELOPER PAIN RADAR // Loading...

    Developers complain.
    Opportunities appear.

    We track what developers are struggling with today — and what opportunities that creates.

    —
    guides today
    —
    avg happiness
    🔥 Pain
    📖 Guides
    🔭 Explore
    👤 Mine
    🔥 Pain Radar — rage scores today
    ↗ share
    💡 Opportunity Feed — pain = market gap
    📈 Tool Momentum
    all scores →
    📖 Latest Guide
    all guides →
    📖 All Guides
    📊 Tool Scores
    + Submit
    📰 Hacker News
    ➕ Submit a Tool
    ← back