envapt
The apt way to read typed config.
Typed, validated config from process.env, .env files, or any object, on Node, Bun, Deno, Cloudflare Workers, and the browser. No runtime dependencies.
process.env.PORT is a string, and Number(process.env.PORT ?? 3000) turns PORT=80a into NaN with no error. envapt reads each variable as the type you ask for and stops the app at startup when a required one is missing or malformed.
import { Converters, Envapter } from 'envapt';
const { port, databaseUrl, cacheTtl, allowedOrigins } = Envapter.getRequiredAll(
{
PORT: Converters.Port, // number, checked against 0-65535
DATABASE_URL: Converters.Url, // URL
CACHE_TTL: Converters.Time, // "15m" parsed to 900000
ALLOWED_ORIGINS: Converters.array() // string[]
},
'camelCase'
);
If PORT and DATABASE_URL are unset, that call throws one error listing both:
EnvaptError [305]: Missing required environment variables: PORT, DATABASE_URL.
If every key is set but some values can't convert, the call throws one InvalidEnvValue listing every bad key. The message holds keys only, so catch the error and log error.message to keep secret values out of your logs.
Why envapt
- Coming from dotenv. envapt reads the same
.envfiles, picks the ones for the current environment (.env.production,.env.local, and so on), and returns typed values.import 'envapt/config'also copies the keys intoprocess.env, the same asdotenv/config. - A schema when you want one. The built-in converters cover ports, URLs, durations, JSON, emails, and more, with no schema library installed. When a converter isn't enough, pass a zod, valibot, or arktype schema, or any synchronous Standard Schema.
- One API on every runtime. On Node, Bun, and Deno, envapt reads
process.envand your.envfiles from the moment you import it. On Cloudflare Workers, edge runtimes, and the browser, you bind the object the platform gives you once, then read with the same calls. - Functions or decorators. Read with
Envapterfrom any call site, or declare typed class fields with a TC39@Envaptaccessor decorator.
envapt is ESM only and needs Node >=22.23.3, Bun >=1.3.11, or Deno >=2.5. A read with a fallback also returns the fallback when the value can't convert, so PORT=80a reads as 3000. Use a required read for values that must be right.
Install
npm install envapt
pnpm add envapt
yarn add envapt
bun add envapt
deno add jsr:@materwelon/envapt
require('envapt') throws ERR_PACKAGE_PATH_NOT_EXPORTED. In a CommonJS project, load it with await import('envapt').
Quick start
Functional
On Node, Bun, and Deno, read from any call site with no setup:
import { Converters, Envapter } from 'envapt';
import { z } from 'zod';
const requestTimeout = Envapter.getAs('REQUEST_TIMEOUT', Converters.Time, '30s'); // 30000
const maxRetries = Envapter.getAs('MAX_RETRIES', Converters.Integer, 3);
const debug = Envapter.getAs('DEBUG', Converters.Boolean, false);
const logLevel = Envapter.getRequired('LOG_LEVEL', z.enum(['debug', 'info', 'warn', 'error']));
On Cloudflare Workers, bind env once in a config module, before the first read:
import { env } from 'cloudflare:workers';
import { Converters, Envapter, PortableSource } from 'envapt';
Envapter.useSource(new PortableSource(env));
export const apiToken = Envapter.getRequired('API_TOKEN', Converters.String);
In the browser, bind the object your bundler injects, such as Vite's import.meta.env.
Decorator
Declare config as class fields with a TC39 accessor decorator. TypeScript needs no extra compiler flag for it, and Bun and Deno run it from .ts directly.
import { Converters, Envapt } from 'envapt';
class Config {
@Envapt('PORT', { converter: Converters.Port, fallback: 3000 })
static accessor port: number;
@Envapt('CACHE_TTL', { converter: Converters.Time, fallback: '15m' })
static accessor cacheTtl: number;
}
For experimentalDecorators projects, the legacy decorators are in envapt/legacy.
Coming from dotenv
Swap import 'dotenv/config' for this line. Your process.env.X reads keep working while you move them over:
import 'envapt/config';
Docs
AI coding tools can use the right API with the envapt agent skill. Install it with npx skills add materwelonDhruv/envapt, or point your tool at the published skill.
Built by @materwelonDhruv · Apache 2.0