Hacker News new | ask | show | jobs
by reaktivo 1043 days ago
There’s no guarantee one is set right?
5 comments

There’s also no guarantee the application will be able to reach a database, bind to a port, or read something off of a file system.
You can use validator like populate-env[1] to check for missing env variables

[1] https://www.npmjs.com/package/populate-env

A type system doesn't really solve that issue tho.
A "runtime type system" does, meaning something like io-ts or zod.

It also allows statically typing and transforming env strings into much more useful and complex structures.

No more so than that any other kind of config file is present and complete, I suppose.
I don't know that this is the best way, but I use a module that instantiates a proxy object. It populates the config object behind the proxy at startup and crashes with a useful message if a value is missing. If something tries to get an undefined value at runtime, the proxy also throws an error. This way the module documents the config, and the app crashes if something is missing rather than silently trying to run with an undefined value. You can also use object.freeze or what have you to make the config object immutable for most practical purposes.
I don't know if that's the best way either, but it's the model all the most successful engineering cultures I've been part of have used, which certainly says a lot for it in my view at least.
Typescript types them to be string | undefined.