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

Env vars undefined in Netlify Functions? Deploy netlify.toml with [functions] config section + redeploy.

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

TL;DR

Cause: Netlify Functions can't access environment variables without explicit function configuration in netlify.toml. Fix: Add [functions] section to netlify.toml and redeploy from Git (not CLI deploy).

---

Real Console Error Messages

You'll see one of these at 2am:

``` undefined is not a valid AWS Lambda environment variable value ```

``` ReferenceError: process.env.DATABASE_URL is not defined at Runtime.handler (/var/task/src/functions/query.js:5:15) ```

``` TypeError: Cannot read property 'split' of undefined at Object.<anonymous> (/var/task/src/functions/api.js:12:3) ```

``` ✗ Functions bundled with errors: - .netlify/functions/db: process.env.API_KEY returned undefined ```

``` netlify dev: Environment variable DATABASE_URL is set but function cannot access it ```

---

Broken vs. Fixed Code

❌ BROKEN: Missing netlify.toml function config

```toml

netlify.toml (incomplete)

[build] command = "npm run build" functions = "netlify/functions" ```

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

✅ FIXED: With explicit function configuration

```toml

netlify.toml (complete)

[build] command = "npm run build" functions = "netlify/functions"

[functions] node_bundler = "esbuild" external_node_modules = ["aws-sdk"] ```

```javascript // netlify/functions/query.js (same code, now works) exports.handler = async (event) => { const dbUrl = process.env.DATABASE_URL; // DEFINED ✓ const apiKey = process.env.API_KEY; // DEFINED ✓ return { statusCode: 200, body: JSON.stringify({ db: dbUrl?.substring(0, 10) }) }; }; ```

Key difference: The [functions] section tells Netlify to properly inject env vars during build.

---

Why This Happens

Netlify's function deployment has two paths:

1. CLI deploy (netlify deploy --prod): Env vars from .env.production don't get injected into function code 2. Git-triggered deploy (GitHub/GitLab push): Works IF netlify.toml has [functions] section

Without explicit [functions] configuration, Netlify treats function bundling as optional, and skips env var injection.

Version note: This behavior applies to Netlify Functions (AWS Lambda runtime) as of 2026. If you're using Edge Functions instead, see [Edge Functions environment setup guide](/?guide=netlify-edge-env).

---

Complete Solution Checklist

✓ Step 1: Update netlify.toml ```toml [functions] node_bundler = "esbuild" ```

✓ Step 2: Set env vars in Netlify UI

  • Site settings → Build & deploy → Environment
  • Add DATABASE_URL, API_KEY, etc.
  • ✓ Step 3: Push to Git (don't use netlify deploy CLI) ```bash git add netlify.toml git commit -m "Add functions config" git push origin main ```

    ✓ Step 4: Verify in Deploy logs Netlify UI → Deploys → Latest → Functions section should show green checkmarks

    ✓ Step 5: Test locally first ```bash npm install -g netlify-cli@latest netlify dev

    Visit http://localhost:8888/.netlify/functions/query

    ```

    ---

    Still broken? Check these too

    1. Typo in env var names — process.env.database_url (lowercase) won't match DATABASE_URL (uppercase). Check Site settings → Environment variables for exact spelling.

    2. Using netlify deploy instead of Git push — The CLI deploy doesn't trigger the same build process as Git-connected deploys. Remove site from "Linked sites" and reconnect via GitHub/GitLab, then push commits only.

    3. Function bundler mismatch — If using ESM imports, you may need node_bundler = "esbuild" explicitly. If using CommonJS with optional dependencies, add external_node_modules = ["module-name"] to prevent bundling errors that mask env var issues.

    See also: [Debugging Netlify Function cold starts](/?guide=netlify-cold-start)

    ---

    Official Resources

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

    Found a different variation? Drop it in the comments — If your error looks different or the fix didn't work, share your error message and netlify.toml content below. We update this guide based on real production issues.

    🔥 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