Netlify: env vars not loading in functions [2026 fix]
Netlify functions can't access env vars because they're not deployed to the functions context. Deploy with netlify.toml config pointing to correct build directory.
Netlify: env vars not loading in functions [2026 fix]
TL;DR
Cause: Environment variables are set in Netlify UI but functions runtime doesn't have access because functions aren't being bundled/deployed correctly or netlify.toml is misconfigured.Fix: Add [functions] block to netlify.toml with correct directory, rebuild, and redeploy—env vars will auto-inject at runtime.
---
Real Console Error Messages
When you hit your function endpoint, you'll see one of these:
``` undefined is not a function at Runtime.handler (checking process.env returns undefined) ```
``` TypeError: Cannot read property 'API_KEY' of undefined at Object.<anonymous> (/var/task/index.js:12:15) ```
``` {"errorMessage":"2022-11-14T06:15:32.123Z undefined ERROR Invoke Error {\"errorMessage\":\"process.env.DATABASE_URL is undefined\"} ```
``` Error: ENOENT: no such file or directory, open '.env' at Object.openSync (fs.js:...) at Runtime.handler ```
``` 503 Service Unavailable - The Lambda function failed to execute Event: {"errorMessage":"Variable missing: MY_SECRET_VAR"} ```
---
Broken Code → Fixed Code
Problem Setup
Your netlify.toml (BROKEN): ```toml [build] command = "npm run build" publish = "dist"
Missing! Functions aren't being found
```Your function (functions/api.js): ```javascript exports.handler = async (event, context) => { const apiKey = process.env.API_KEY; // Returns undefined const dbUrl = process.env.DATABASE_URL; // Returns undefined return { statusCode: 200, body: JSON.stringify({ apiKey, dbUrl }) }; }; ```
Solution
Your netlify.toml (FIXED): ```toml [build] command = "npm run build" publish = "dist"
[functions] directory = "netlify/functions" node_bundler = "esbuild" ```
Your function structure: ``` project/ ├── netlify/ │ └── functions/ │ └── api.js ├── netlify.toml └── package.json ```
Your function (netlify/functions/api.js) — FIXED: ```javascript exports.handler = async (event, context) => { // These now work because netlify.toml [functions] block is set const apiKey = process.env.API_KEY; const dbUrl = process.env.DATABASE_URL; // Optional: Add safety check if (!apiKey || !dbUrl) { return { statusCode: 500, body: JSON.stringify({ error: "Missing env vars" }) }; } return { statusCode: 200, body: JSON.stringify({ apiKey: apiKey.substring(0, 5) + "...", dbUrl }) }; }; ```
Env vars in Netlify UI (already set):
API_KEY = your-key-hereDATABASE_URL = postgres://...---
Why This Happens
1. Default functions location mismatch: Netlify defaults to netlify/functions/ but if your netlify.toml is missing the [functions] block or points elsewhere, functions aren't deployed.
2. Legacy function structure: Older projects used .netlify/functions/ or functions/ at root—modern Netlify prefers netlify/functions/.
3. Build output isolation: If functions live in dist/ after build, they're excluded from the function runtime context.
4. Version uncertainty: As of 2026, Netlify's function runtime behavior with build artifacts varies. Explicit netlify.toml config is always the safest approach.
---
Still Broken? Check These Too
1. Wrong build directory: If publish = "dist" but functions are in src/functions/, Netlify won't find them. Always mirror: if functions are at project root netlify/functions/, keep them OUT of the build publish directory. Netlify deploys them separately.
2. Missing npm run build or build step: If your build command doesn't complete, functions don't get bundled. Check build logs: Site → Deploys → Click recent deploy → View deploy log. Look for Functions section showing "Bundled X functions."
3. Function file syntax errors: If your .js has a typo (like missing closing brace), the bundler fails silently in older Netlify versions. Test locally: npm install -g netlify-cli then netlify functions:invoke api to test before pushing.
---
Quick Checklist
netlify.toml has [functions] block with directory = "netlify/functions"netlify/functions/, NOT in src/ or build outputBundled N functions under the Functions sectionnetlify dev to simulate production env var injection---
Related Guides
---
Official Resources
---
Found a different variation? Drop it in the comments.