Skip to content

Repository files navigation

@juriadams/env

Validate npm version bundle size

A tiny, native, type-safe library for reading and validating environment variables, optimized for Workers, Bun, and Node.

Installation

bun add @juriadams/env

Usage

import { vars, optional } from "@juriadams/env";

const env = vars([
  "DB_URL",
  "OPENAI_API_KEY",
  optional("SENTRY_DSN"),
  optional("PORT", "3000"),
]);

type Env = typeof env;
// {
//   DB_URL: string;
//   OPENAI_API_KEY: string;
//   SENTRY_DSN: string | null;
//   PORT: string | "3000";
// }

Optional Variables

optional("PORT") marks a key as optional. A default value can be provided via optional("PORT", "3000"), the type of which is strictly inferred.

Important

If one or more expected environment variables are missing, a MissingEnvironmentVariablesError is thrown.

Custom Environment

By default, vars reads from import.meta.env and process.env (in order). A custom environment to read from can be provided via vars([ ... ], { env: process.env }).

Important

If no environment can be resolved, an InvalidEnvironmentError is thrown.

Lifecycle

Develop

bun dev

Test

bun test

Build

bun run build

Typecheck

bun typecheck

Lint / Format

bun lint
bun format

About

A simple and type-safe way to validate environment variables.

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages