envyak

v0.1.1, zero dependencies, 2.6 kBNodeBunDenoEdge

Typed environment variables. Every error at once.

Describe your configuration once. envyak reads it, parses it into real numbers, booleans, URLs and dates, infers the exact TypeScript type, and when something is wrong it tells you about every variable in one message, without ever printing a secret.

Trail permit$ npm i envyak
A yak carrying labeled crates of configuration up the trail PORT URL KEY
2.6kBmin+gzip, the whole library
0runtime dependencies
12validators, one env()
183tests, 100% line coverage

01 Playground

Edit the .env, watch the types follow

This runs the real library in your browser against the sample schema. Break something and the error report updates as you type.

Route check

One checkpoint per variable. Pick a flag to read its log.

Summit clear

Tab inserts two spaces. Press Escape, then Tab, to move focus out of the editor.


              
No errors. Break a line in the editor to see the report.
// typeof config, inferred with no annotations
type Config = {
  readonly PORT: number;
  readonly DEBUG: boolean;
  readonly DATABASE_URL: string;
  readonly NODE_ENV: 'development' | 'production' | 'test';
  readonly API_KEY: string;
  readonly TAGS: readonly string[] | undefined;
  readonly RATIO: number;
  readonly START: Date;
  readonly CONFIG: unknown;
  readonly ADMIN_EMAIL: string;
};

Secret values stay masked when the object is logged or serialized. The real value is still there for your code.

02 Why it exists

Configuration fails at the worst moment

One error at a time

Most setups crash on the first missing variable. You fix it, redeploy, and meet the next one. envyak checks the whole schema and lists every problem with a fix hint.

Secrets in the logs

A failed check that prints the value it rejected can leak an API key into your log pipeline. Variables marked secret are never printed, not in errors and not when the config object is logged.

Strings pretending to be numbers

process.env only holds strings. envyak gives you numbers, booleans, URLs, lists, JSON and dates, with types inferred exactly: optional means T | undefined, default means T.

03 How to

How to validate environment variables in Node.js

Three steps, no config files and no schema library to learn.

  1. Install the package

    One package with no dependencies. It ships ESM, CommonJS and its own types.

    Trail permit$ npm i envyak
  2. Describe every variable

    Pick a validator for each key and add a default, an example or secret: true where it helps.

    config.js7 lines
    import { env, port, url, str } from 'envyak';
    
    export const config = env({
      PORT: port({ default: 3000 }),
      DATABASE_URL: url(),
      API_KEY: str({ secret: true }),
    });
  3. Import the typed config

    Read config.PORT as a number anywhere in your app. If something is wrong, startup stops with one clear report.

    server.js4 lines
    import { config } from './config.js';
    
    server.listen(config.PORT);
    // config.PORT is a number, typed as number

04 Features

Small enough to forget, strict enough to trust

01

Exact inference

No generics to write. oneOf gives a union of literals, list gives a readonly array, optional adds undefined only when there is no fallback.

02

Every error, one throw

EnvError lists each missing or invalid variable with what was expected, what was received and how to fix it.

03

Secrets stay secret

secret: true masks the value in errors, toString, JSON.stringify and util.inspect, in Node, Bun and Deno.

04

Zero dependencies

2.6 kB min+gzip for the whole library, less when your bundler drops the validators you do not import.

05

Any runtime

Reads process.env by default, falls back to Deno.env, and takes any object as source: Workers bindings, import.meta.env, a parsed file.

06

Strict by design

bool accepts exactly true, false, 1, 0, yes, no, on and off. Numbers reject hex and Infinity. Dates must be real calendar dates.

07

Dev defaults

devDefault fills a value while you develop and makes it required when NODE_ENV is production.

08

Frozen result

The config object, its lists and its JSON values are frozen, so nothing rewrites your settings at runtime.

05 API

Twelve validators and one function

Every validator takes the shared options default, devDefault, optional, description, example and secret.

ValidatorAcceptsType
str()any non-empty string, minLength, maxLength, patternstring
num()finite decimal number, min, maxnumber
int()safe integer in base 10, min, maxnumber
port()integer from 1 to 65535number
bool()true false 1 0 yes no on off, any caseboolean
url()absolute URL, optional protocolsstring
email()one @, no spaces, a dot in the domainstring
oneOf([...])one of the listed strings'a' | 'b'
list(item)separated items, each parsed by a validatorreadonly T[]
json<T>()any JSON text, deep frozenT
date()ISO 8601 date or date timeDate
custom(fn)whatever your function returnsR
config.tsenv()
import { env, port, bool, url, oneOf, str } from 'envyak';

export const config = env({
  PORT: port({ default: 3000 }),
  DEBUG: bool({ default: false }),
  DATABASE_URL: url(),
  NODE_ENV: oneOf(['development', 'production', 'test']),
  API_KEY: str({ secret: true }),
});

config.PORT;     // number
config.NODE_ENV; // 'development' | 'production' | 'test'
String(config);  // {"PORT":3000,...,"API_KEY":"[secret]"}
terminalEnvError
EnvError: 3 environment variables are missing or invalid:

  DATABASE_URL  is missing
                fix: set DATABASE_URL, for example DATABASE_URL=https://example.com
  PORT          expected a port number from 1 to 65535, got "70000"
                fix: set PORT, for example PORT=3000
  API_KEY       expected a string of at least 32 characters, got a secret value (hidden)
                fix: set API_KEY to a string of at least 32 characters

Same schema, any runtime

server.jsNode 18+
// node --env-file=.env server.js
import { env, port, url, str, bool } from 'envyak';

export const config = env({
  PORT: port({ default: 3000 }),
  DATABASE_URL: url({ protocols: ['postgres', 'postgresql'] }),
  SESSION_SECRET: str({ secret: true, minLength: 32 }),
  DEBUG: bool({ devDefault: true, default: false }),
});

// CommonJS works too: const { env, port } = require('envyak');

06 Benchmarks

Fast enough to run on every cold start

Parsing the same 8 variables, median of 5 runs. Higher is better. Node v24.16.0 on Windows 10 x64, a modest desktop.

Hand written checks are the ceiling: no messages, no masking, no reuse. envyak and @t3-oss/env-core + zod trade places from run to run here; treat them as equally fast.

Size

Minified and gzipped, measured with the same bundler for every library, for a schema with four validators.

envyak
1.8 kB
envalid
2.3 kB
@t3-oss/env-core + zod/mini
4.5 kB
@t3-oss/env-core + zod
93.2 kB
znv + zod
94.3 kB

Compared honestly

Checked against the published packages. Each of them does something envyak does not, so pick what fits.

Compared honestlyenvyakenvalid 9t3 env-coreznv
Runtime dependenciesnonenonea Standard Schema libraryzod (peer)
Reports every erroryesyesyesyes
Masks secret valuesyesnonono
Type inferenceyesyesyesyes
On failurethrows EnvErrorprints, then exits the processlogs issues, throwsthrows a formatted list
Unique strengthsecret masking, exact types with no schema librarystrict proxy that throws on unknown keysclient and server split for frameworksthe full power of zod schemas

07 Compatibility

Runs where your code runs

Node.js 18 and later, ESM and CommonJS

Bun, tested in CI

Deno 2, tested in CI

Edge runtimes and Workers, pass the source

Browsers and bundlers, tree shakeable

TypeScript 5.0 and later for the types

08 FAQ

Questions

09 Summit

Pack your config for the climb

Install envyak, describe your variables once, and let every deploy tell you exactly what is missing before it ever reaches production.