Exactly which .env files get read, in which order, and how do I confirm it?
Asked 2,040 views3 answers
9votes
I have inherited a repo with .env, .env.local, .env.staging and .env.staging.local at the root and two more inside the project folder. Nobody remembers why.
Before I delete any of them: what is the full list of names that are read, and is there a way to see which ones a given run actually picked up?
Read in that order, each merged over the last, missing ones skipped. So the bottom of the list wins, and the whole stack still loses to an exported variable.
To see which ones a run actually read:
bash
sdods config show -p demo-shop -e staging
The second line is the list of dotenv files, or (none).
Before deleting, diff them — the usual reason for six files is that two people solved the same problem in different places and both work, so neither noticed.
Convention that has held up for us: .env.<env> is committed to nothing but documented in .env.example and shared with the team; .env.local is personal and never mentioned to anyone. If a file does not fit one of those two descriptions it can go.
Late addition for anyone finding this: the same eight paths are checked whether or not the project folder differs from the repo root. In a single-project repo where the two are the same directory, only the four names are read once — you do not get duplicates.