@knpwrs/envariant
v1.1.1
Published
An Invariant for Environment Variables
Downloads
297
Maintainers
Readme
envariant
This is a small snippet that I found myself writing for multiple projects in
the form of an npm module. envariant
is essentially a way to guarantee that
an environment variable contains a truthy string at runtime. If the environment
variable is not defined, envariant
will throw. This is useful for type
narrowing your environment variables that you expect to be defined.
Usage
import env from '@knpwrs/envariant';
const DB_URI = env('DB_URI');
// DB_URI is guaranteed to be defined at this point, as long as there is a `DB_URI` environment variable defined.
// If there isn't a `DB_URI` variable defined then `env` will throw and this script will crash.
// There is an optional second parameter that lets you supply an object to read variables from; for
// instance, the `env` parameter in Cloudflare workers:
const ENDPOINT = env('ENDPOINT', env);
Installation
npm install @knpwrs/envariant