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

Netlify Functions can't access env vars because they're defined in UI but not deployed to function runtime. Redeploy after setting vars or use netlify.toml.

TL;DR

Cause: Environment variables set in Netlify UI aren't automatically injected into Function runtime without a fresh deploy.

Fix: Either redeploy your site after adding env vars, or define them in netlify.toml with [functions] context.

---

Real Error Messages You'll See

``` ReferenceError: process is not defined at Object.<anonymous> (/var/task/index.js:5:23) ```

``` TypeError: Cannot read property 'MY_API_KEY' of undefined at handler (/var/task/functions/api.js:12:8) ```

``` Error: process.env.DATABASE_URL is undefined at connectDB (file:///var/task/index.mjs:3:15) ```

``` {"errorMessage":"Cannot destructure property 'API_TOKEN' of 'process.env' as it is undefined."} ```

``` netlify functions:invoke myFunction -- --no-identity Error: Environment variable STRIPE_KEY not found ```

---

Broken Code vs. Fixed Code

❌ Broken: Relying only on UI vars without redeploy

```javascript // functions/charge.js exports.handler = async (event) => { const stripeKey = process.env.STRIPE_SECRET_KEY; // You added STRIPE_SECRET_KEY in Netlify UI // but never triggered a redeploy if (!stripeKey) { return { statusCode: 500, body: 'Key missing' }; } return { statusCode: 200, body: stripeKey }; }; ```

✅ Fixed: Define in netlify.toml + redeploy

```toml

netlify.toml

[functions] node_bundler = "esbuild"

[[context.production.env]] name = "STRIPE_SECRET_KEY" value = "sk_live_xxx"

[[context.development.env]] name = "STRIPE_SECRET_KEY" value = "sk_test_yyy" ```

```javascript // functions/charge.js (same code, now works) exports.handler = async (event) => { const stripeKey = process.env.STRIPE_SECRET_KEY; if (!stripeKey) { return { statusCode: 500, body: 'Key missing' }; } return { statusCode: 200, body: stripeKey }; }; ```

Then redeploy: ```bash git add netlify.toml git commit -m "Add env vars to toml" git push origin main ```

Alternative: UI-only vars (requires redeploy)

1. Go to Site settings → Build & deploy → Environment 2. Add variable: STRIPE_SECRET_KEY = sk_live_xxx 3. Click Trigger deploy (don't just save) 4. Wait for build to complete 5. Test function—vars now available

---

Why This Happens

Netlify Functions run in isolated Lambda-like containers. Environment variables must be baked into the deployment bundle OR passed at invoke time. Setting them in the UI creates them for your *build process* but older deployment bundles won't have them. Functions already deployed before the env var was added won't see the new variables until you redeploy.

I'm uncertain whether this behavior changed between Netlify CLI v17 vs v18+. Test locally with netlify functions:invoke to verify your setup before pushing.

---

Still Broken? Check These Too

1. Local vs. Production Mismatch

Problem: Works with netlify dev but not in production. Solution:
  • Verify you're testing the correct context (production vs development)
  • Use netlify functions:invoke --context=production to test production env vars locally
  • Check Site settings to confirm vars are set (not just in toml)
  • 2. Function Not Being Deployed

    Problem: Function file exists locally but missing from deployment. Solution:
  • Ensure functions are in netlify/functions/ folder (or folder specified in netlify.toml [build] functions field)
  • Run netlify deploy --prod --dry-run to see what's being deployed
  • Check for .netlifyignore excluding your functions folder
  • 3. ESM vs CommonJS Module Format

    Problem: process.env works in CommonJS but not ES modules. Solution:
  • CommonJS: const key = process.env.MY_VAR
  • ESM: import { env } from 'process'; const key = env.MY_VAR or use process.env directly (Netlify supports both)
  • Verify node_bundler setting in netlify.toml matches your code style
  • ---

    Official Resources

  • [Netlify Environment Variables Documentation](https://docs.netlify.com/configure-builds/environment-variables/)
  • [Netlify Functions Guide](https://docs.netlify.com/functions/overview/)
  • ---

    Related Guides

  • [Build step secrets not available in functions](/?guide=netlify-build-secrets)
  • [Fix Netlify function timeouts during cold starts](/?guide=netlify-cold-start)
  • ---

    Action Checklist for 2am

  • [ ] Did you add env var to Netlify UI? Yes → Trigger redeploy now
  • [ ] Does netlify.toml exist? No → Create it with [functions] context
  • [ ] Run netlify env:list locally—see your vars? No → Re-authenticate with netlify login
  • [ ] Check function file extension—is it .js or .mjs? Confirm bundler setting
  • [ ] Redeploy and wait 2 minutes for Lambda cold start
  • ---

    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