A tiny, native, type-safe library for reading and validating environment variables, optimized for Workers, Bun, and Node.
bun add @juriadams/envimport { 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("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.
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.
bun devbun testbun run buildbun typecheckbun lint
bun format