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

Env vars undefined in Netlify functions? Deploy netlify.toml with [functions] context or use process.env after build step.

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

TL;DR

Cause: Environment variables aren't being passed to serverless functions because netlify.toml lacks a [functions] configuration block or variables aren't set in the correct Netlify UI context.

Fix: Add [functions] section to netlify.toml with node_bundler = "esbuild" and redeploy, OR ensure env vars are set in Netlify UI under Site settings > Build & deploy > Environment (not just local .env).

---

Exact Error Messages

You'll likely see one of these in your function logs:

``` TypeError: Cannot read property 'STRIPE_API_KEY' of undefined ```

``` ReferenceError: process is not defined ```

``` [Functions] Error: SUPABASE_URL is undefined when calling database ```

``` Error: process.env.DATABASE_PASSWORD returned null - connection failed ```

``` 2am alert: Function invoked but returned {error: "API_KEY not found"} ```

---

The Problem: Before vs After

❌ BROKEN CODE

netlify/functions/api.js ```javascript exports.handler = async (event) => { const apiKey = process.env.STRIPE_API_KEY; const dbUrl = process.env.DATABASE_URL; console.log('Keys:', apiKey, dbUrl); // logs: undefined, undefined return { statusCode: 500, body: JSON.stringify({ error: 'Missing env vars' }) }; }; ```

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

✅ FIXED CODE

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

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

[[redirects]] from = "/api/*" to = "/.netlify/functions/:splat" status = 200 ```

netlify/functions/api.js (unchanged, but now works) ```javascript exports.handler = async (event) => { const apiKey = process.env.STRIPE_API_KEY; const dbUrl = process.env.DATABASE_URL; console.log('Keys loaded:', apiKey.substring(0, 8) + '...'); // works! return { statusCode: 200, body: JSON.stringify({ success: true }) }; }; ```

Then in Netlify UI: 1. Go to Site settings > Build & deploy > Environment 2. Click Add environment variables 3. Set STRIPE_API_KEY=sk_live_... and DATABASE_URL=postgres://... 4. Redeploy (git push or "Deploy site" button)

---

Why This Happens

Netlify doesn't automatically inject env vars into function bundles unless you:

1. Define the functions directory in [functions] block 2. Set env vars in Netlify UI (local .env files never reach production) 3. Trigger a redeploy after adding env vars 4. Use the correct bundler (esbuild is recommended for Node 18+)

Note on version specificity: We're assuming Netlify Functions Node 18.x (default as of 2025-2026). If using Node 16 or earlier, node_bundler defaults to esbuild but you should specify it anyway. We're uncertain if this behavior changes with Netlify's 2026 runtime updates—check their [official functions docs](https://docs.netlify.com/functions/overview/) for the latest.

---

Verification Checklist

  • [ ] netlify.toml contains [functions] section
  • [ ] directory in [functions] matches your file structure (default: netlify/functions)
  • [ ] Env vars are set in Netlify UI (not just .env.local)
  • [ ] You hit the Redeploy button after adding env vars
  • [ ] Function file is in correct directory: netlify/functions/*.js
  • [ ] Local testing works with .env file (confirms code logic)
  • [ ] Check function logs in Netlify UI: Functions > View logs
  • ---

    Still broken? Check these too

    1. [Netlify build context mismatch](/?guide=netlify-contexts) — You set env vars for production but deployed to preview branch. Set vars for all contexts you use.

    2. [ESBuild bundler stripping process.env](/?guide=esbuild-env-stripping) — If using dynamic env var access (process.env[key]), esbuild may tree-shake it. Use explicit strings: process.env.SPECIFIC_KEY.

    3. Function cold starts + stale cache — Clear Netlify's deploy cache: Site settings > Danger zone > Clear site cache, then redeploy.

    ---

    Testing in Production

    After deploying, test immediately:

    ```bash curl https://your-site.netlify.app/.netlify/functions/api ```

    Check Functions > Logs in Netlify UI for real output. Don't rely on local testing—env injection is Netlify-specific.

    ---

    Official Resources

  • [Netlify Functions Environment Variables Docs](https://docs.netlify.com/functions/overview/?fn-language=js#environment-variables)
  • [netlify.toml Reference](https://docs.netlify.com/configure-builds/file-api-reference/)
  • ---

    Found a different variation? Drop it in the comments—especially if you're hitting this on Netlify's Edge Functions or with specific frameworks like Next.js/Remix.

    🔥 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