envmagic
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

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

Package Sidebar

Install

npm i envmagic

Weekly Downloads

3

Version

0.1.2

License

MIT

Unpacked Size

14.8 kB

Total Files

11

Last publish

Collaborators

  • blackmarket