Introduction
Read environment variables as typed values, and stop the app at startup when one is missing or malformed.
Every value in process.env is a string, so you've written lines like these before:
const = (.. ?? 3000);
const = .. === 'true';
const = (.. ?? 60_000);
const = ..;Each line breaks on a value you didn't expect:
portisNaNforPORT=80a, and0forPORT=because??keeps an empty string.debugisfalseforDEBUG=1.cacheTtlisNaNforCACHE_TTL=10m.databaseUrlis astring | undefined, so you still check it and parse the URL yourself.
None of these stop the app. It starts with the bad value and fails later, somewhere far from the line that read it. Every file that reads one of these variables also carries its own copy of the line.
Stop at startup on a bad value
getRequiredAll reads a group of variables and converts each one to the type you ask for. If any are missing or can't be converted, it throws one error that lists every bad key:
export const = .({
: .,
: .,
: .
});
.CACHE_TTL;$ PORT=80a CACHE_TTL=soon DATABASE_URL=postgres://db/app node config.ts
EnvaptError [210]: Validation failed for PORT, CACHE_TTL.PORT=80a and CACHE_TTL=soon both show up in that one error, so you fix both before the next deploy. When the values are good, config.PORT is a number, config.DATABASE_URL is a URL, and CACHE_TTL=10m comes back as 600000 milliseconds. If your entry file imports config first, a bad deploy stops before the app serves anything.
Fall back to a default
Some values have a safe default, like a debug flag or a timeout. Envapter.getAs takes the key, a converter, and the default:
const debug = .('DEBUG', ., false);const = .('REQUEST_TIMEOUT', ., '30s');Converters.Boolean reads 1, yes, true, and on as true, so DEBUG=1 gives true. timeout is 30000 when REQUEST_TIMEOUT is unset. A fallback also removes undefined from the type, so debug is a boolean, while a read with no fallback, like Envapter.getAs('DATABASE_URL', Converters.Url), is URL | undefined and you handle the missing case yourself.
The default also covers a value that can't convert, so REQUEST_TIMEOUT=soon gives 30000 with no error. Use getRequiredAll for anything where a typo should stop the app. Fallbacks and missing values has the exact rules.
Reading through envapt is optional, and code that reads process.env directly keeps working. A script that reads one string can stay on process.env. envapt helps most once several files read config, since one call gives each read its type, its default, and its check.
Functions or decorators
Envapter reads one value per call, as above. The @Envapt decorator reads a variable into a class field. A decorated field reads on first access and keeps that value until the cache is rebuilt, for example by a useSource call (When values are read). A reader call converts again each time.
class {
@('PORT', { : ., : 3000 })
static accessor : number;
@('REDIS_URL', { : ., : new ('redis://localhost:6379') })
static accessor : ;
}Use a decorator for settings that many files read, since the class declares each one once and Config.port works anywhere. Use a reader for a one-off read, or for a check that has to run at startup, since a decorated field reads only when something first accesses it. The cost is the accessor keyword on every field, which Decorators explains. Both forms share the same converters and accept a Standard Schema validator such as zod, valibot, or arktype. A decorator takes the schema through its schema option.
Where values come from
A source is the object envapt reads values from. On Node, Bun, and Deno the source is process.env plus your .env files. It binds when you import envapt, so the first read works with no setup. If other code reads process.env directly, import envapt/config to put your .env values there too (.env files on Node).
On Workers, in the browser, and on edge runtimes, envapt reads nothing until you bind a source. Your config arrives there as an object, such as the Workers env or import.meta.env in a bundler like Vite. Wrap that object in a PortableSource and pass it to Envapter.useSource once, before the first read. If a read runs before that, it throws NoSourceBound. Sources shows the full setup.
Where to start
Node, Bun, and Deno
Install envapt and read a typed value from your .env file.
Cloudflare Workers
Bind the Worker env object once and read typed config.
Browser
Read typed values from the config your bundler injects.
Edge runtimes
Bind the platform's config object on Vercel Edge, Fastly, and similar.
Upgrading from v8
What changed in v9 and how to upgrade.