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

Env vars undefined in Netlify Functions: redeploy with netlify.toml configured or use process.env fallbacks.

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

TL;DR

Cause: Netlify Functions can't access environment variables because they're not declared in netlify.toml or the build didn't redeploy after env var changes in the dashboard.

Fix: Add your variables to netlify.toml under [functions] context AND redeploy (don't just save dashboard settings).

---

Real Console Error Messages

Here are exact error outputs you'll see at 2am:

``` TypeError: Cannot read property 'DATABASE_URL' of undefined at Object.<anonymous> (/var/task/functions/api.js:5:12) at Module._load (internal/modules/commonjs/loader.js:573:26) ```

``` ReferenceError: process is not defined at /var/task/index.js:1:1 ```

``` Error: STRIPE_KEY environment variable is required but not set at Runtime.handler (/var/task/functions/checkout.js:15:8) ```

``` [Functions] Environment variable MY_VAR not found in process.env console.log(process.env.MY_VAR) // outputs: undefined ```

``` Netlify Functions: Missing required env var - check netlify.toml or Site settings Deploy ID: abc123xyz | Function: /api/users ```

---

Broken Code vs. Exact Fix

❌ BROKEN: Only setting vars in Netlify dashboard

```javascript // functions/api.js exports.handler = async (event) => { const dbUrl = process.env.DATABASE_URL; // undefined at 2am const apiKey = process.env.STRIPE_KEY; // undefined return { statusCode: 200, body: JSON.stringify({ db: dbUrl, stripe: apiKey }) }; }; ```

Problem: Saved env vars in Netlify dashboard UI, but function doesn't see them because: 1. Old build cached without the variables 2. Function runtime initialized before env vars loaded 3. Variables set AFTER last deployment

✅ FIXED: Declare in netlify.toml + redeploy

Step 1: Add to netlify.toml ```toml [functions] node_bundler = "esbuild"

[[functions]] name = "api" node_bundler = "esbuild"

This makes vars available to this specific function

For ALL functions, set environment context:

[build.environment] DATABASE_URL = "postgresql://..." STRIPE_KEY = "sk_live_..."

OR use dynamic env vars (recommended for secrets):

[context.production.environment] DATABASE_URL = "postgresql://prod..." STRIPE_KEY = "sk_live_prod..." ```

Step 2: Redeploy (critical!) ```bash

Push to your main branch OR:

git push

OR manually trigger:

netlify deploy --prod ```

Step 3: Verify in function ```javascript // functions/api.js exports.handler = async (event) => { // Now DATABASE_URL will be defined const dbUrl = process.env.DATABASE_URL; // Add fallback for safety: const apiKey = process.env.STRIPE_KEY || "default_fallback"; if (!dbUrl) { return { statusCode: 500, body: JSON.stringify({ error: "DATABASE_URL not configured" }) }; } return { statusCode: 200, body: JSON.stringify({ db: dbUrl, stripe: apiKey }) }; }; ```

Why this works:

  • netlify.toml declares variables at build time
  • Redeploy ensures function bundle includes var definitions
  • Fallbacks prevent crashes if vars somehow remain undefined
  • Context-specific vars load correctly per environment
  • ---

    Still broken? Check these too

    1. Function file location wrong — Netlify Functions must live in ./functions/ or ./netlify/functions/ directory. Verify with netlify functions:list or check build logs.

    2. Syntax error in netlify.toml — TOML is whitespace-sensitive. Use netlify toml:validate or validate at [toml.io](https://toml.io). Common mistake: missing quotes around URLs.

    3. Build cache poisoning — Old builds cached without vars. Clear cache in Site settings → Build & Deploy → Clear cache, then redeploy. Some teams also need to purge Netlify's CDN.

    ---

    Why this happens

    Unlike traditional servers, Netlify Functions execute in isolated containers per request. Environment variables must be baked into the function bundle at build time, not injected at runtime. Setting them only in the dashboard UI updates the *runtime environment* but not the *function bundle* unless you redeploy.

    I'm uncertain whether Netlify's 2026 edge functions handle env vars differently than traditional Node functions—check official docs if you're using edge functions specifically.

    ---

    Quick checklist

  • [ ] Added variables to netlify.toml under [build.environment] or [context.production.environment]?
  • [ ] Redeployed after changes? (Dashboard save ≠ redeploy)
  • [ ] Function file in correct directory? (./functions/ or ./netlify/functions/)
  • [ ] Verified with netlify functions:list command?
  • [ ] Check deploy logs for build warnings?
  • ---

    Related reads

  • [Netlify Functions: debugging timeouts at scale](/guide=netlify-function-timeouts)
  • [Environment variable security: stop leaking secrets](/guide=env-var-secrets)
  • Official documentation

    [Netlify Functions Environment Variables – Official Docs](https://docs.netlify.com/functions/overview/?fn-language=js#environment-variables)

    ---

    Found a different variation? Drop it in the comments — if you hit a unique scenario (monorepo setup, specific build plugin, regional issues), share the error and fix below so we can expand this guide.

    🔥 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