Your .env Is Not Loading: The Five Usual Reasons

Symptom

process.env.VAR is undefined despite the file being right there.

Root cause

Wrong directory, wrong name, syntax errors, or platform substitution eating the value.

Fix, in order

1. Load with an explicit pathrequire('dotenv').config({ path: __dirname + '/.env' })
2. No spaces, no quotes unless needed, export-freeAPI_KEY=abc123 # not export API_KEY=abc123
3. In compose: env_file vs inline substitution differenv_file: - .env # versus ${VAR} resolved by compose itself
4. Docker does not copy dotfiles by defaultCOPY .env . # .dockerignore may exclude it

Prevent it coming back

Fail fast at boot if required vars are missing.

Run it on clean infra

Spinning up a fresh box to reproduce or escape this error?

DevOps tools shelf

Related

Cloud & DevOps tool comparisons · Hardware build guides

NinjaOps publishes free engineering guides. Some outbound links are affiliate links: they cost you nothing and support the site.

Last reviewed 2026-10-02 · NinjaOps SEO grid · DevOps blog