Skip to content

API Reference

createEnv(schema, options?)

Creates a strongly typed environment object based on the provided schema.

Parameters

ParameterTypeDescription
schemaEnvSchemaObject mapping variable names to their configuration
optionsEnvOptions (optional)Global options (env files, prefix, error handler, etc.)

Returns

  • InferEnv<S> — a fully typed object with parsed environment variables.
  • WatchableEnv<S> — when watch: true, extends InferEnv<S> with refresh(), on(), and off().

Throws

  • Error if any variable fails validation and no onError handler is provided.
  • Error if freeze and watch are both set.

EnvOptions

PropertyTypeDefaultDescription
envFilesboolean | string[]falseLoad .env files before validation
prefixstringundefinedPrefix prepended when reading env variables
onError(errors: string[]) => voidundefinedCustom error handler replaces default throw
strictbooleanfalseProxy throws on access to unknown keys
freezebooleanfalseObject.freeze the returned object
watchtrueundefinedReturn a watchable env with refresh()
validate(env: InferEnv<S>) => boolean | stringundefinedCross-field validation after per-field checks pass. Return true to pass, false for a generic error, or a string as the error message.

freeze and watch are mutually exclusive (type-level and runtime).


EnvVarConfig

Configuration for a single scalar environment variable. One of the following variant shapes:

Common Fields

FieldTypeDescription
type"string" | "number" | "boolean"Expected data type
requiredboolean | ((env: NodeJS.ProcessEnv) => boolean)Fail if missing; function receives full process.env for conditional required
defaultstring | number | booleanFallback value
describestringHuman-readable description for error messages
sensitivebooleanRedact value in errors and change events
coerce(raw: string) => unknownCustom coercion before type parsing

Variant-Specific Fields

These are mutually exclusive — only one of the following can be set per variable:

FieldTypeDescription
choicesreadonly (string | number | boolean)[]Fixed set of allowed values
validate(value: string | number | boolean) => booleanCustom validation function
format"url" | "email" | "ip" | "port" | "uuid"Built-in format preset (string only)

EnvArrayConfig

Configuration for an array environment variable.

FieldTypeDescription
type"array"Must be "array"
itemType"string" | "number" | "boolean"Type of each array element
separatorstringDelimiter (default ",")
requiredboolean | ((env: NodeJS.ProcessEnv) => boolean)Fail if missing; supports conditional function
defaultstring[]Fallback value
describestringHuman-readable description
sensitivebooleanRedact in errors and change events
coerce(raw: string) => unknownCustom coercion, bypasses split logic

EnvGroup

A nested group of variables. Any schema key whose value is a plain object (without a type string property) is treated as a group:

ts
type EnvGroup = Record<string, EnvVarConfig>;

The group name is upper-cased and used as an env-var prefix: dbDB_HOST, DB_PORT.


WatchableEnv<S>

Returned when watch: true. Extends InferEnv<S> with:

MethodDescription
refresh(): voidRe-read env, re-validate, fire change events
on("change", listener): voidRegister a change listener
off("change", listener): voidRemove a change listener

ChangeListener<S>

ts
type ChangeListener<S> = (
  key: keyof S & string,
  oldValue: unknown,
  newValue: unknown,
) => void;

InferEnv<S>

Utility type that infers the output shape from a schema:

ts
type InferEnv<S extends EnvSchema> = {
  [K in keyof S]: /* inferred type based on config */
};
  • required: true → non-nullable
  • With default → non-nullable
  • Optional without default → T | undefined
  • choices: [...] as const → literal union type
  • Group keys → nested object

Framework Adapters

All framework adapters share the same signature:

ts
function createXxxEnv<C extends EnvSchema, S extends EnvSchema>(
  config: FrameworkEnvConfig<C, S>,
): FrameworkEnv<C, S>;

FrameworkEnvConfig<C, S>

FieldTypeDescription
clientC extends EnvSchemaSchema for client-side (public) vars
serverS extends EnvSchemaSchema for server-side (private) vars
optionsOmit<EnvOptions, "prefix">Shared options (prefix is auto-set)

FrameworkEnv<C, S>

ts
interface FrameworkEnv<C, S> {
  client: InferEnv<C>;
  server: InferEnv<S>;
}

Available Adapters

FunctionClient prefix
createNextEnvNEXT_PUBLIC_
createViteEnvVITE_
createAstroEnvPUBLIC_
createSvelteKitEnvPUBLIC_
createRemixEnv(none)

Standalone Utilities

loadEnvFiles(files: string[]): void

Load the specified .env files into process.env. Existing values are never overwritten.

defaultEnvFiles(): string[]

Returns the default file list for the current NODE_ENV:

ts
defaultEnvFiles();
// → [".env", ".env.production", ".env.local"]

Released under the MIT License.