Envapter
Reads typed environment variables on Node, Bun, and Deno. It adds the filesystem-only configuration
APIs: .env path selection, base directory, dotenv options, and per-environment profiles. On the
portable build (Workers, the browser, edge) these same APIs warn once and do nothing, unless
Envapter.fileApiMode is 'throw'.
Accessors
baseDir
Get Signature
get static baseDir(): string | undefined;The configured base directory, or undefined when relative paths resolve against the working directory.
See
https://envapt.materwelon.dev/docs/env-files#reading-from-a-fixed-directory
Returns
string | undefined
Set Signature
set static baseDir(value): void;Set the base directory that relative .env paths resolve against. Without one they resolve against
process.cwd(). It covers the auto-cascade, Envapter.configureProfiles paths, and relative envPaths.
Absolute paths always bypass it. Pass a directory path (import.meta.dirname) or a module URL
(import.meta.url) to resolve paths next to the calling file regardless of launch directory.
Set this before envPaths so relative envPaths validate against the right directory.
Setting undefined restores process.cwd() resolution.
See
https://envapt.materwelon.dev/docs/env-files#reading-from-a-fixed-directory
Parameters
| Parameter | Type |
|---|---|
value | string | URL | undefined |
Returns
void
debug
Get Signature
get static debug(): DebugLevel;Returns
Set Signature
set static debug(level): void;Set the debug log level. Defaults to silent. When unset, reads ENVAPT_DEBUG from the
bound source on first access. The setter overrides any env-var value. Output goes to stderr
on Node (the console elsewhere), prefixed with [envapt].
See
https://envapt.materwelon.dev/docs/errors#debug-logging
Parameters
| Parameter | Type |
|---|---|
level | DebugLevel |
Returns
void
Inherited from
Envapter.debugenvFileOptions
Get Signature
get static envFileOptions(): EnvFileOptions;The env file loader options.
See
https://envapt.materwelon.dev/docs/env-files#file-options
Returns
Set Signature
set static envFileOptions(config): void;Set the env file loader options (encoding, override). Refreshes the cache. Any other key
throws InvalidUserDefinedConfig.
See
https://envapt.materwelon.dev/docs/env-files#file-options
Parameters
| Parameter | Type |
|---|---|
config | EnvFileOptions |
Returns
void
environment
Get Signature
get static environment(): Environment;The current application environment, detected on first read unless you set it.
See
https://envapt.materwelon.dev/docs/environment#detecting-the-environment
Returns
Set Signature
set static environment(env): void;Set the application environment to an Environment value such as 'production', matched
case-insensitively. An unknown name selects 'development'. Setting it reloads the .env files
for that environment.
See
https://envapt.materwelon.dev/docs/environment#detecting-the-environment
Parameters
| Parameter | Type |
|---|---|
env | Environment | string & object |
Returns
void
Inherited from
Envapter.environmentenvPaths
Get Signature
get static envPaths(): string[];The configured .env file paths.
See
https://envapt.materwelon.dev/docs/env-files#choosing-the-files-yourself
Returns
string[]
Set Signature
set static envPaths(paths): void;Set custom .env file paths. Setting new paths clears the cache and reloads environment
variables. Throws EnvFilesNotFound when a path does not exist.
Once set, these paths replace the dotenv-flow auto-cascade and any Envapter.configureProfiles
paths.
See
https://envapt.materwelon.dev/docs/env-files#choosing-the-files-yourself
Parameters
| Parameter | Type |
|---|---|
paths | string | string[] |
Returns
void
fileApiMode
Get Signature
get static fileApiMode(): FileApiMode;Returns
Set Signature
set static fileApiMode(mode): void;On the portable build, this controls the filesystem-only config APIs (envPaths, baseDir,
envFileOptions, configureProfiles, resetPaths). 'warn' (the default) warns once and
does nothing. 'throw' throws EnvaptError FileApiUnsupported. The node build runs these
APIs normally and this value has no effect there.
See
https://envapt.materwelon.dev/docs/compatibility#file-apis-and-fileapimode
Parameters
| Parameter | Type |
|---|---|
mode | FileApiMode |
Returns
void
Inherited from
Envapter.fileApiModeisDevelopment
Get Signature
get static isDevelopment(): boolean;Whether the current environment is development.
See
https://envapt.materwelon.dev/docs/environment#branching-on-the-environment
Returns
boolean
Inherited from
Envapter.isDevelopmentisProduction
Get Signature
get static isProduction(): boolean;Whether the current environment is production.
See
https://envapt.materwelon.dev/docs/environment#branching-on-the-environment
Returns
boolean
Inherited from
Envapter.isProductionisStaging
Get Signature
get static isStaging(): boolean;Whether the current environment is staging.
See
https://envapt.materwelon.dev/docs/environment#branching-on-the-environment
Returns
boolean
Inherited from
Envapter.isStagingisTest
Get Signature
get static isTest(): boolean;Whether the current environment is test.
See
https://envapt.materwelon.dev/docs/environment#branching-on-the-environment
Returns
boolean
Inherited from
Envapter.isTeststrict
Get Signature
get static strict(): boolean;Returns
boolean
Set Signature
set static strict(value): void;When true, a whitespace-only value counts as missing, a ${VAR} that can't resolve throws
MissingEnvValue, and an empty array element throws EmptyArrayElement. Default false.
Setting it clears and rebuilds the cache.
See
https://envapt.materwelon.dev/docs/required-values#strict-mode
Parameters
| Parameter | Type |
|---|---|
value | boolean |
Returns
void
Inherited from
Envapter.strictsyncProcessEnv
Get Signature
get static syncProcessEnv(): boolean;Returns
boolean
Set Signature
set static syncProcessEnv(value): void;When true, copies dotenv-loaded keys into process.env. Default false.
envFileOptions.override controls collisions because only keys the loader wrote are copied.
With the default false, existing process.env values stay. With true, the file value
replaces them in both the cache and process.env.
Flipping false → true copies the loaded keys immediately without a cache refresh.
Flipping true → false leaves copied keys in process.env until the process exits.
See
https://envapt.materwelon.dev/docs/env-files#file-options
Parameters
| Parameter | Type |
|---|---|
value | boolean |
Returns
void
Inherited from
Envapter.syncProcessEnvMethods
configureProfiles()
static configureProfiles(config): void;Configure per-environment .env path overrides on top of the dotenv-flow auto-cascade.
When set, each Environment key's paths load ahead of the cascade for that environment and
win over it, unless envFileOptions.override is true. Unspecified environments still use the
cascade as-is.
Set useDefaults: false to disable the cascade entirely (load only the configured paths).
Setting an explicit Envapter.envPaths value at any point overrides this configuration.
Parameters
| Parameter | Type |
|---|---|
config | ProfilesConfig |
Returns
void
Example
Envapter.configureProfiles({
staging: { paths: 'config/staging.env' },
production: { paths: ['config/prod.env', 'secrets/prod.env'] }
});See
https://envapt.materwelon.dev/docs/env-files#profiles
get()
static get<Default>(key, fallback?): ConditionalReturn<string, Default>;Get a string environment variable, or fallback when it is missing or empty. Expands
${VAR} templates in the value. Accepts a single key or an ordered array of keys, read until
one has a value. Read any other type with getAs and a converter.
Type Parameters
Parameters
| Parameter | Type |
|---|---|
key | EnvKeyInput |
fallback? | Default |
Returns
ConditionalReturn<string, Default>
See
https://envapt.materwelon.dev/docs/envapter#strings-and-typed-values
Inherited from
Envapter.getgetAs()
Call Signature
static getAs<TConverter, TFallback>(
key,
converter,
fallback?
): AdvancedConverterReturn<TConverter, TFallback>;Get an environment variable through a converter: a built-in token such as Converters.Number,
an ArrayOf token from Converters.array(...), a custom (raw: string) => T function, or a
synchronous Standard Schema (zod, valibot, arktype). A missing value returns the fallback, or
undefined without one. A scalar token or a function that can't convert the value returns the
fallback too. A bad array element throws ArrayElementConversionFailed. A schema that rejects
the value throws SchemaValidationFailed. envapt never runs the schema on the fallback. The key
can be a single name or an ordered list, read until one key has a value.
A bad converter or fallback throws even when the value is missing.
Type Parameters
| Type Parameter | Default type |
|---|---|
TConverter extends ConverterToken | ArrayOf<ArrayElement> | - |
TFallback extends | string | number | bigint | boolean | RegExp | string[] | JsonArray | JsonObject | URL | Date | unknown[] | number[] | bigint[] | boolean[] | URL[] | Date[] | TimeFallback[] | object[] | null | undefined | undefined |
Parameters
| Parameter | Type |
|---|---|
key | EnvKeyInput |
converter | TConverter |
fallback? | TFallback |
Returns
AdvancedConverterReturn<TConverter, TFallback>
See
- https://envapt.materwelon.dev/docs/envapter#other-types-with-getas
- https://envapt.materwelon.dev/docs/converters#custom-converters
Inherited from
Envapter.getAsCall Signature
static getAs<TReturn>(
key,
converter,
fallback
): TReturn;Get an environment variable through a converter: a built-in token such as Converters.Number,
an ArrayOf token from Converters.array(...), a custom (raw: string) => T function, or a
synchronous Standard Schema (zod, valibot, arktype). A missing value returns the fallback, or
undefined without one. A scalar token or a function that can't convert the value returns the
fallback too. A bad array element throws ArrayElementConversionFailed. A schema that rejects
the value throws SchemaValidationFailed. envapt never runs the schema on the fallback. The key
can be a single name or an ordered list, read until one key has a value.
A bad converter or fallback throws even when the value is missing.
Type Parameters
Parameters
| Parameter | Type |
|---|---|
key | EnvKeyInput |
converter | "json" |
fallback | TReturn |
Returns
TReturn
See
- https://envapt.materwelon.dev/docs/envapter#other-types-with-getas
- https://envapt.materwelon.dev/docs/converters#custom-converters
Inherited from
Envapter.getAsCall Signature
static getAs<TReturn>(
key,
converter,
fallback?
): TReturn | undefined;Get an environment variable through a converter: a built-in token such as Converters.Number,
an ArrayOf token from Converters.array(...), a custom (raw: string) => T function, or a
synchronous Standard Schema (zod, valibot, arktype). A missing value returns the fallback, or
undefined without one. A scalar token or a function that can't convert the value returns the
fallback too. A bad array element throws ArrayElementConversionFailed. A schema that rejects
the value throws SchemaValidationFailed. envapt never runs the schema on the fallback. The key
can be a single name or an ordered list, read until one key has a value.
A bad converter or fallback throws even when the value is missing.
Type Parameters
Parameters
| Parameter | Type |
|---|---|
key | EnvKeyInput |
converter | "json" |
fallback? | TReturn |
Returns
TReturn | undefined
See
- https://envapt.materwelon.dev/docs/envapter#other-types-with-getas
- https://envapt.materwelon.dev/docs/converters#custom-converters
Inherited from
Envapter.getAsCall Signature
static getAs<Schema, TFallback>(
key,
schema,
fallback?
): ConditionalReturn<InferSchemaOutput<Schema>, TFallback>;Get an environment variable through a converter: a built-in token such as Converters.Number,
an ArrayOf token from Converters.array(...), a custom (raw: string) => T function, or a
synchronous Standard Schema (zod, valibot, arktype). A missing value returns the fallback, or
undefined without one. A scalar token or a function that can't convert the value returns the
fallback too. A bad array element throws ArrayElementConversionFailed. A schema that rejects
the value throws SchemaValidationFailed. envapt never runs the schema on the fallback. The key
can be a single name or an ordered list, read until one key has a value.
A bad converter or fallback throws even when the value is missing.
Type Parameters
| Type Parameter | Default type |
|---|---|
Schema extends StandardSchemaV1<unknown, unknown> | - |
TFallback extends unknown | undefined |
Parameters
| Parameter | Type |
|---|---|
key | EnvKeyInput |
schema | SchemaConstraint<Schema> |
fallback? | TFallback |
Returns
ConditionalReturn<InferSchemaOutput<Schema>, TFallback>
See
- https://envapt.materwelon.dev/docs/envapter#other-types-with-getas
- https://envapt.materwelon.dev/docs/converters#custom-converters
Inherited from
Envapter.getAsCall Signature
static getAs<TReturn, TFallback>(
key,
converter,
fallback?
): ConditionalReturn<NonNullable<TReturn>, TFallback>;Get an environment variable through a converter: a built-in token such as Converters.Number,
an ArrayOf token from Converters.array(...), a custom (raw: string) => T function, or a
synchronous Standard Schema (zod, valibot, arktype). A missing value returns the fallback, or
undefined without one. A scalar token or a function that can't convert the value returns the
fallback too. A bad array element throws ArrayElementConversionFailed. A schema that rejects
the value throws SchemaValidationFailed. envapt never runs the schema on the fallback. The key
can be a single name or an ordered list, read until one key has a value.
A bad converter or fallback throws even when the value is missing.
Type Parameters
Parameters
| Parameter | Type |
|---|---|
key | EnvKeyInput |
converter | ConverterFunction<TReturn> |
fallback? | TFallback |
Returns
ConditionalReturn<NonNullable<TReturn>, TFallback>
See
- https://envapt.materwelon.dev/docs/envapter#other-types-with-getas
- https://envapt.materwelon.dev/docs/converters#custom-converters
Inherited from
Envapter.getAsgetRaw()
static getRaw(key): string | undefined;Read an environment variable as its raw string, skipping parsing and conversion. A ${NAME}
reference in the value comes back unexpanded.
Parameters
| Parameter | Type |
|---|---|
key | EnvKeyInput |
Returns
string | undefined
See
https://envapt.materwelon.dev/docs/envapter#the-raw-string
Inherited from
Envapter.getRawgetRequired()
Call Signature
static getRequired<TConverter>(key, converter): InferConverterReturnType<TConverter>;Read a required environment variable and convert it, throwing MissingEnvValue when the value
is missing or empty. A value it cannot convert throws InvalidEnvValue with issues keyed by
the env name. A bad array element, an error a custom parser or schema throws, and a schema's
own issues all become issues on that error. Accepts a built-in or ArrayOf token, a custom
parser function, or a synchronous Standard Schema. The key can be a single name or an ordered list.
A bad converter throws before MissingEnvValue.
Type Parameters
Parameters
| Parameter | Type |
|---|---|
key | EnvKeyInput |
converter | TConverter |
Returns
InferConverterReturnType<TConverter>
See
https://envapt.materwelon.dev/docs/required-values#getrequired-require-and-has
Inherited from
Envapter.getRequiredCall Signature
static getRequired<Schema>(key, schema): InferSchemaOutput<Schema>;Read a required environment variable and convert it, throwing MissingEnvValue when the value
is missing or empty. A value it cannot convert throws InvalidEnvValue with issues keyed by
the env name. A bad array element, an error a custom parser or schema throws, and a schema's
own issues all become issues on that error. Accepts a built-in or ArrayOf token, a custom
parser function, or a synchronous Standard Schema. The key can be a single name or an ordered list.
A bad converter throws before MissingEnvValue.
Type Parameters
| Type Parameter |
|---|
Schema extends StandardSchemaV1<unknown, unknown> |
Parameters
| Parameter | Type |
|---|---|
key | EnvKeyInput |
schema | SchemaConstraint<Schema> |
Returns
InferSchemaOutput<Schema>
See
https://envapt.materwelon.dev/docs/required-values#getrequired-require-and-has
Inherited from
Envapter.getRequiredCall Signature
static getRequired<TReturnType>(key, converter): NonNullable<TReturnType>;Read a required environment variable and convert it, throwing MissingEnvValue when the value
is missing or empty. A value it cannot convert throws InvalidEnvValue with issues keyed by
the env name. A bad array element, an error a custom parser or schema throws, and a schema's
own issues all become issues on that error. Accepts a built-in or ArrayOf token, a custom
parser function, or a synchronous Standard Schema. The key can be a single name or an ordered list.
A bad converter throws before MissingEnvValue.
Type Parameters
Parameters
| Parameter | Type |
|---|---|
key | EnvKeyInput |
converter | ConverterFunction<TReturnType> |
Returns
NonNullable<TReturnType>
See
https://envapt.materwelon.dev/docs/required-values#getrequired-require-and-has
Inherited from
Envapter.getRequiredgetRequiredAll()
static getRequiredAll<Spec, Casing>(spec, casing?): { [K in string | number | symbol as RecaseKey<K & string, Casing>]: InferSpecField<Spec[K]> };Read a group of required environment variables in one call. Each key in spec maps to a
converter (a token, an array() token, or a custom parser) or a synchronous Standard Schema
(zod, valibot, arktype, etc). The returned record holds every value. A converter field is
never undefined. A schema field holds whatever its schema returns.
A bad converter in spec throws first. envapt then collects every missing or empty key and
throws one MissingEnvValue listing them all. envapt never runs the schema for a missing key,
so a schema default such as zod's .default() does not apply here.
If no key is missing, envapt reads every field and throws one InvalidEnvValue for all
the bad values, schema fields included. Its issues hold every failed field's issues, each
with its env key first in path. A converter that cannot convert adds the issue
could not be converted. A bad array element and an error a custom parser or schema throws
each become an issue carrying their message.
The error message lists only the keys, since an issue message can quote the value.
Pass a casing ('camelCase', 'PascalCase', or 'kebab-case') to rename the record keys.
The rename splits each key on underscores, as in SCREAMING_SNAKE env-var names. With no casing
the keys stay as-is.
Type Parameters
Parameters
| Parameter | Type |
|---|---|
spec | Spec & SpecSchemaConstraint<Spec> |
casing? | Casing |
Returns
{ [K in string | number | symbol as RecaseKey<K & string, Casing>]: InferSpecField<Spec[K]> }
See
https://envapt.materwelon.dev/docs/required-values
Inherited from
Envapter.getRequiredAllhas()
static has(key): boolean;Check whether key has a value. An unset or empty value counts as missing, the same as in
getRequired. Under strict mode, a value whose ${VAR} can't resolve counts as missing too,
and an ordered key list returns false at that key without checking the later ones.
Parameters
| Parameter | Type |
|---|---|
key | EnvKeyInput |
Returns
boolean
Example
const cache = Envapter.has('REDIS_URL') ? createRedisCache() : createMemoryCache();See
https://envapt.materwelon.dev/docs/required-values#getrequired-require-and-has
Inherited from
Envapter.hasrequire()
static require(...keys): void;Assert that one or more environment variables are present and non-empty after template
resolution. Throws MissingEnvValue listing every missing key. A whitespace-only value
counts as missing only under strict mode. Each argument can be an ordered key list, which
passes when any of its keys has a value.
For a typed required read in functional code, use Envapter.getRequired(key, converter).
Parameters
| Parameter | Type |
|---|---|
...keys | [EnvKeyInput, ...EnvKeyInput[]] |
Returns
void
Example
Envapter.require('DATABASE_URL');
Envapter.require('DATABASE_URL', 'API_KEY', 'SENTRY_DSN');
Envapter.require(['READONLY_URL', 'DATABASE_URL'], 'API_KEY');Inherited from
Envapter.requireresetPaths()
static resetPaths(): void;Clears any Envapter.configureProfiles call, any explicit Envapter.envPaths assignment, and any
environment set through Envapter.environment. Envapter.baseDir keeps its value. The call
reloads the dotenv-flow cascade right away, choosing the files from the environment set in your
env vars.
Returns
void
See
https://envapt.materwelon.dev/docs/env-files#profiles
resolve()
static resolve(strings, ...keys): string;Tagged template literal for resolving environment variables in template strings. A missing
value resolves to an empty string. In strict mode an empty or whitespace-only value throws
MissingEnvValue.
Parameters
| Parameter | Type |
|---|---|
strings | TemplateStringsArray |
...keys | EnvKeyInput[] |
Returns
string
Example
// Given API_HOST=api.example.com and API_PORT=8080 in environment
const endpoint = Envapter.resolve`Connecting to ${'API_HOST'}:${'API_PORT'}`;
// Returns: "Connecting to api.example.com:8080"
// Works with template variables in .env too:
// API_URL=https://${API_HOST}:${API_PORT}
const message = Envapter.resolve`Service endpoint: ${'API_URL'}`;
// Returns: "Service endpoint: https://api.example.com:8080"
// reads READONLY_URL when present, otherwise DATABASE_URL
const db = Envapter.resolve`Connecting to ${['READONLY_URL', 'DATABASE_URL']}`;See
https://envapt.materwelon.dev/docs/templates#the-resolve-tagged-template
Inherited from
Envapter.resolveuseSource()
static useSource(source): void;Bind the environment Source. On Node the entry binds FileSource for you
(a process.env snapshot plus the .env cascade). On the browser or Workers, pass a
PortableSource (or any Source) before reading. Pass a (key) => string | undefined reader for
a source that reads one key at a time and cannot list its keys. Clears and rebuilds the cache.
Parameters
| Parameter | Type |
|---|---|
source | | Source | ((key) => string | undefined) |
Returns
void
See
https://envapt.materwelon.dev/docs/sources#the-sources-envapt-ships
Inherited from
Envapter.useSource