Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
andybarron/envZero-dependency environment variable parsing in Node, Deno, and Bun
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers




JSR Score
100%
Published
11 months ago (0.3.1)
@andyb/env
Zero-dependency environment variable parsing in Node, Deno, and Bun
Installation
Tool | Install command |
---|---|
Bun | bunx jsr add @andyb/env |
Deno | deno add @andyb/env |
NPM | npx jsr add @andyb/env |
PNPM | pnpm dlx jsr add @andyb/env |
Yarn | yarn dlx jsr add @andyb/env |
Quickstart
import * as env from "@andyb/env"; // parse() is compatible with process.env in Node and Deno.env in Deno. // On failure, the thrown error will report every variable that failed to parse. const config = parse(process.env, { // Specify expected type and environment variable name. favoriteNumber: env.integer().variable("FAVORITE_NUMBER"), // Mark some environment variables as optional. They will only be parsed if present. nickname: env.string().variable("NICKNAME").optional(), // Default values can be provided. This makes the variable optional as well. lovesDeno: env.boolean().variable("LOVES_DENO").default(true), // If no variable is specified, the property name will be used. TZ: env.string(), }); // config will have its type inferred correctly: type InferredType = { favoriteNumber: number; nickname: string | undefined; lovesDeno: boolean; TZ: string; };
Custom parsers
import * as env from "@andyb/env"; import ms from "ms"; // To parse custom types, provide a description and a parser function. function duration() { return env.custom( 'must be a duration e.g. "10 seconds"', (value: string): number => ms(value), ); } const config = parse(process.env, { // Custom parsers have the same chainable configuration methods as the // built-in parsers. timeoutMs: duration().variable("TIMEOUT").optional(), }); // Type inference works for custom and async parsers as well: type InferredType = { timeoutMs: number; healthCheck: number; };
Built-in parser types
import * as env from "@andyb/env"; const config = env.parse(process.env, { BOOLEAN: env.boolean(), // only accepts "true" and "false" INTEGER: env.integer(), JSON: env.json(), // accepts any valid JSON value NUMBER: env.number(), PORT: env.port(), // accepts integers from 0 to 65545 STRING: env.string(), }); type InferredType = { BOOLEAN: boolean; INTEGER: number; JSON: env.JsonValue; NUMBER: number; PORT: number; STRING: string; };
Built and signed on
GitHub Actions
Add Package
deno add jsr:@andyb/env
Import symbol
import * as env from "@andyb/env";
Import directly with a jsr specifier
import * as env from "jsr:@andyb/env";
Add Package
pnpm i jsr:@andyb/env
pnpm dlx jsr add @andyb/env
Import symbol
import * as env from "@andyb/env";
Add Package
yarn add jsr:@andyb/env
yarn dlx jsr add @andyb/env
Import symbol
import * as env from "@andyb/env";
Add Package
npx jsr add @andyb/env
Import symbol
import * as env from "@andyb/env";
Add Package
bunx jsr add @andyb/env
Import symbol
import * as env from "@andyb/env";