Stop Hardcoding, Start Centralizing We've all been there: sitting at the top of a random file.
Then someone pushes it to production and the app starts talking to staging.
Or worse, you have ten files scattered across microservices with slightly different variable names.
This is a config nightmare.
The clean way is to treat environment configuration as a first-class concern: centralize it, validate it, and access it through a typed, consistent interface.
Here's how I do it in Node.js, and the pattern translates to any language.
The Core Principle: One Place, One Truth All environment variables should be loaded in a single module, transformed into a structured object, and exported.
No other file should read directly.
This gives you a single place to add defaults, validate types, and document what's available.
Step 1: Load and Parse First, I use to load a file in development (never commit it, but do commit a ).
Then I read variables with sensible defaults.
Note the calls.
Environment variables are always strings, so converting to numbers here prevents bugs later.
Also, I'm grouping related settings into nested objects, which keeps the export clean.
Step 2: Validate Early, Fail Fast Missing required variables should crash the app at startup, not halfway through a request.
I use a tiny validation function or a library like .
Here's a manual check that's easy to read: If you want a more declarative approach, is great.
It gives you types, defaults, and validation in one shot.
Step 3: Use a Config Service in Your App Now, instead of importing everywhere, you import your config object.
For example, in an Express app: In a database client: This makes your code testable too.
In tests, you can override with a mock object, no need to mess with .
Step 4: Keep Secrets Out of Code Never hardcode secrets like API keys or passwords.
Use environment variables, and for production, use a secrets manager (AWS Secrets Manager, Vault, etc.) that injects them into the environment at runtime.
Your config module doesn't care where they come from; it just reads them.
Step 5: Document with a Commit a file so other developers know what to set.
Include comments for each variable.
What About Other Languages?
The same pattern applies everywhere.
In Python, use or .
In Go, use or .
The idea is universal: centralize, validate, and expose a typed config object.
The Payoff No more magic strings scattered across the codebase.
Startup fails fast if required config is missing.
Easy to test by mocking the config module.
Onboarding is simpler with a clear .
It's a small investment that pays off every time you deploy to a new environment or debug a configuration issue.
Your future self will thank you.
Further Reading Node.js dotenv documentation MDN: Environment variables (general concept) Happy configuring!