Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP1stack specificStack: node
nodeenvconfigerror-handlingdx
Cascade env-file lookup or fail loudly with an explanatory error
When loading env config, try a sensible cascade of files and, if a required one is missing, throw a clear error explaining what is expected.
PR: hegnar-forum-web · org-mining-hist-2026-06Created: Jun 20, 2026
Bad example
Old codejs
| 1 | require('dotenv').config({ path: '.env.development.local' }); |
| 2 | // silently does nothing if the file is absent |
Explanation (EN)
Objašnjenje (HR)
Good example
New codejs
| 1 | const { existsSync } = require('fs'); |
| 2 | const assert = require('assert'); |
| 3 | const candidates = ['.env.development.local', '.env.local', '.env']; |
| 4 | const found = candidates.find(existsSync); |
| 5 | assert(found, `Expected one of env files to exist: ${candidates.join(', ')}`); |
| 6 | require('dotenv').config({ path: found }); |
Explanation (EN)
Objašnjenje (HR)