Skip to content

imaustink/envmagic

Repository files navigation

Env Magic

Automated Tests NPM version

Manage your environment variables like magic. Just import env from envmagic and use it like you would process.env. By default, accessing undefined or empty environment variables throws an error.

import { env } from "envmagic";

// Throws an error if NODE_ENV is undefined or ""
const { NODE_ENV } = env;

Motivation

I think we've all seen and written code that looks a lot like this.

import assert from "node:assert";

const { FOO } = process.env;

assert(FOO, "FOO is required!");

This is error prone, ugly, and can easily become outdated. It's time to automate your environment variable access.

Customization

Env Magic aims to be highly customizable. You may import the configuration object and change setting in your application code, or you can set most setting via environment variables.

Property Env Default Description
onMissing MAGIC_ENV_ON_MISSING error Action taken for missing (undefined) variables
onEmpty MAGIC_ENV_ON_EMPTY error Action taken for missing ("") variables
optional MAGIC_ENV_OPTIONAL [] Variables for which no action should be taken
logger n/a console.warn Custom logger function
import { configuration } from "envmagic";

configuration.set("onMissing", "warn");
configuration.set("onEmpty", "warn");
configuration.set("logger", (message) => {
  // implement custom logger here
});
configuration.set("optional", ["OPTIONAL_VARIABLE"]);

Actions

Actions are the things that Env Magic can do in response to missing or empty environment variables.

  • error: Throws an error indicating a given variable is missing or empty
  • warn: Logs a warning indicating a given variable is missing or empty
  • null: Returns null when the given environment variable is missing or empty
  • undefined: Returns undefined when the given environment variable is missing or

About

Manage your environment variables like magic

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published