Why is process.env.VARIABLE undefined?
Common causes: dotenv not loaded (add require('dotenv').config() at the top), variable not in .env file, typo in variable name, or .env file not in the right location (dotenv looks in process.cwd()). Use debug: true to see what dotenv is loading.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in dotenv Troubleshooting Common Issues Undefined Variables and Loading Problems
dotenv must be loaded before any module that uses process.env. If a config module is imported before require('dotenv').config(), the variables will be undefined. Load dotenv at the very top of your entry file (server.js or app.js).
Use quotes: JWT_SECRET="my secret with spaces". For multi-line values (like private keys), use double quotes. For values with # (which dotenv interprets as comments), wrap in quotes: API_KEY="abc#123".
Use dotenv-flow (require('dotenv-flow').config()) which automatically loads .env, .env.local, .env.${NODE_ENV}, and .env.${NODE_ENV}.local. Or specify the path manually: require('dotenv').config({ path: '.env.production' }). Ensure NODE_ENV is set before loading.
Still have questions?
Browse all our FAQs or reach out to our support team
