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

Environment variables undefined in Netlify Functions? Missing netlify.toml config or .env file in functions folder causes it.

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 under [functions] context or your .env file isn't in the correct directory.

Fix: Add environment variable declarations to your netlify.toml file and ensure variables are accessible in the functions build context.

---

Exact Error Messages from Console

Here are real console outputs you'll see when this breaks at 2am:

``` Error: Cannot read property 'undefined' of undefined at Object.<anonymous> (/var/task/function.js:5:12) ```

``` TypeError: process.env.STRIPE_KEY is undefined at handler (/var/task/index.js:42:15) ```

``` ReferenceError: REACT_APP_API_URL is not defined at Object.<anonymous> (/var/task/api.js:8:5) ```

``` [Functions] Server is ready. Listening on port 34567 Warning: environment variable OPENAI_API_KEY was not found ```

``` FunctionError: Failed to parse environment variables during build Status: 500 Error details: process.env is null in function context ```

---

Broken Code vs. Exact Fix

Problem 1: Missing netlify.toml Declaration

❌ BROKEN:

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

✅ FIXED:

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

[functions] node_bundler = "esbuild"

[[environment]] context = "production" [environment.environment] STRIPE_KEY = "sk_live_xyz" API_URL = "https://api.example.com"

[[environment]] context = "deploy-preview" [environment.environment] STRIPE_KEY = "sk_test_abc" API_URL = "https://staging-api.example.com" ```

Problem 2: Function File Not Accessing Variables Correctly

❌ BROKEN:

```javascript // netlify/functions/process-payment.js const stripe = require('stripe')(STRIPE_KEY);

exports.handler = async (event) => { // STRIPE_KEY is undefined here - not accessing process.env const charge = await stripe.charges.create({ amount: 1000 }); return { statusCode: 200, body: JSON.stringify(charge) }; }; ```

✅ FIXED:

```javascript // netlify/functions/process-payment.js const stripe = require('stripe')(process.env.STRIPE_KEY);

exports.handler = async (event) => { // Verify variable exists before using if (!process.env.STRIPE_KEY) { return { statusCode: 500, body: JSON.stringify({ error: 'STRIPE_KEY not configured' }) }; } const charge = await stripe.charges.create({ amount: 1000 }); return { statusCode: 200, body: JSON.stringify(charge) }; }; ```

Problem 3: .env File in Wrong Location

❌ BROKEN:

``` project/ .env ← Only here, not visible to functions netlify/ functions/ api.js ```

✅ FIXED:

``` project/ .env ← For local dev netlify/ functions/ .env ← Add this for local function testing api.js netlify.toml ← Declare vars here for production ```

Content of netlify/functions/.env: ``` STRIPE_KEY=sk_test_local API_URL=http://localhost:3000 ```

---

Why This Happens

Netlify Functions run in an isolated serverless environment. Unlike your frontend code which can read from a project root .env, functions need variables either:

1. Declared explicitly in netlify.toml (production/preview) 2. Placed in netlify/functions/.env (local dev with netlify dev) 3. Set via Netlify UI: Site Settings → Build & deploy → Environment

I'm uncertain whether Netlify's 2026 runtime still supports reading from project root .env directly in functions—test locally with netlify dev command first.

---

Still broken? Check these too

1. [Netlify Build Context Issues](/guide=netlify-build-context) — Wrong environment context deployed. Verify context field in netlify.toml matches your branch name or custom domain.

2. Functions Node.js Version Mismatch — Netlify's default runtime may differ from your local version. Add node_bundler and specify compatible syntax. Check netlify.toml for node_version setting.

3. [Secrets vs. Config Vars in Netlify UI](/guide=netlify-secrets-vars) — Site settings not synced with netlify.toml. Changes made only in UI don't persist; declare everything in version control.

---

Quick Verification

Run locally first: ```bash netlify dev ```

Then test your function endpoint at http://localhost:8888/.netlify/functions/your-function.

If it works locally but not in production, the issue is the production netlify.toml config or UI settings.

---

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.

    🔥 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