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

Env vars undefined in Netlify Functions? Deploy netlify.toml with [functions] config + restart build. One-line fix inside.

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

TL;DR

Cause: Netlify Functions can't access env vars because netlify.toml lacks [functions] configuration or build didn't redeploy after env var addition. Fix: Add [functions] block to netlify.toml, set env vars in Netlify dashboard, then trigger a new deploy.

---

Exact Console Error Messages

Here are real errors you'll see at 2am:

``` 1. TypeError: Cannot read property of undefined at Runtime.handler (/var/task/functions/api.js:15:23)

2. Error: process.env.DATABASE_URL is undefined at Object.<anonymous> (/var/task/functions/db.js:5:10)

3. ReferenceError: STRIPE_KEY is not defined at module.exports (/var/task/functions/payment.js:8:5)

4. SyntaxError: Unexpected token u in JSON at position 0 (happens when function tries to use undefined env var as JSON)

5. 502 Bad Gateway Function crashed because env var was null during initialization ```

---

Broken Code → Fixed Code (Side by Side)

BROKEN: Missing netlify.toml config

```toml

netlify.toml (incomplete)

[build] command = "npm run build" publish = "dist" ```

FIXED: Add [functions] block

```toml

netlify.toml (correct)

[build] command = "npm run build" publish = "dist"

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

---

BROKEN: Function accessing env var without config

```javascript // netlify/functions/api.js exports.handler = async (event) => { const dbUrl = process.env.DATABASE_URL; const client = new Database(dbUrl); // undefined here! return { statusCode: 200 }; }; ```

FIXED: Ensure netlify.toml + redeploy

```javascript // netlify/functions/api.js (same code, but now env vars load) exports.handler = async (event) => { const dbUrl = process.env.DATABASE_URL; if (!dbUrl) { return { statusCode: 500, body: JSON.stringify({ error: "DATABASE_URL not set" }) }; } const client = new Database(dbUrl); // NOW defined! return { statusCode: 200 }; }; ```

Action: 1. Add [functions] block to netlify.toml 2. Go to Netlify Dashboard → Site Settings → Build & Deploy → Environment 3. Add env vars (e.g., DATABASE_URL=postgres://...) 4. Trigger new deploy: git push or click "Trigger deploy" 5. Wait 2-3 minutes for build to complete

---

Why This Happens

Netlify Functions are serverless (AWS Lambda under the hood). Unlike browser-side code, functions require explicit configuration in netlify.toml to bundle and load environment variables at runtime. Without the [functions] block:

  • Functions deploy but lose env var context
  • process.env returns undefined even if vars are set in dashboard
  • Build succeeds (misleading), but functions crash at runtime
  • Version note: This behavior is consistent across Netlify CLI v11+ and Netlify platform (2024-2026). If you're using legacy netlify-lambda (pre-2020), different setup required—specify in comments if applicable.

    ---

    Still broken? Check these too

    1. Wrong directory path: Confirm functions are in netlify/functions/ (not functions/ or src/functions/). Netlify's default expects netlify/functions/.

    2. Env vars not saved: Click the env var input field, hit Enter, then verify it appears in the list. Some users paste values but forget to confirm. Check dashboard again after 30 seconds—vars should persist.

    3. Local dev mismatch: Running locally with .env file works differently. Install netlify-cli (npm i -g netlify-cli) and run netlify dev instead of npm start. This respects netlify.toml and dashboard env vars. See [local env vars guide](/?guide=netlify-local-dev).

    4. Function cold start timeout: If function still crashes, add timeout handling and verify node_bundler matches your code (esbuild for modern JS, default for Node 18 CommonJS). See [function performance](/?guide=netlify-function-perf).

    ---

    Checklist Before Escalating

  • ✅ netlify.toml has [functions] section?
  • ✅ Env var entered in dashboard *and* hit Enter to save?
  • ✅ Ran git push to trigger deploy (or clicked "Trigger deploy")?
  • ✅ Waited 2+ minutes for deploy badge to turn green?
  • ✅ Function code includes error handling for undefined vars?
  • ✅ Using netlify dev locally, not npm start?
  • ---

    Official docs: [Netlify Functions environment variables](https://docs.netlify.com/functions/overview/#environment-variables)

    ---

    Found a different variation? Drop it in the comments—share your error message and what fixed it for you.

    🔥 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