A wrapper to make dealing with environment variables less painful.
- npm:
npm install @tnezdev/env
- yarn:
yarn add @tnezdev/env
- pnpm:
pnpm add @tnezdev/env
Require this in any file where you need to reference an environment variable. It will throw if the expected key is not defined, otherwise it will return a string.
import { getEnv } from "@tnezdev/env";
import SomeClient from "./clients/some-client";
const configureDefaultClient = () =>
new SomeClient({
username: getEnv("SOME_CLIENT_USERNAME"),
password: getEnv("SOME_CLIENT_PASSWORD"),
});
export default function (client = configureDefaultClient()) {
// ... do something cool ...
}
You can also provide a default value to use if the key is not defined in process.env
:
getEnv("BASE_URL", { default: "http://localhost:3000" });