v8.1.0

Configuration

Control which files load on Node, mirror values back to process.env, and turn on debug logging.

On Node, Bun, and Deno the default source reads process.env plus the .env files it loads. Static settings on Envapter control which files load, whether loaded values reach process.env, and how much envapt logs. They apply only while a Node source is active, on Workers and in the browser you bind a source instead.

. = ['.env.local', '.env'];
. = { : true };
. = true;
. = 'verbose';
NOTE

envapt loads .env files into an isolated copy of process.env, so by default it does not mutate the real process.env. Values are read from envapt's own cache. syncProcessEnv (below) opts into writing them back.

WARNING

Everything on this page reads or writes the filesystem, so it applies to Node, Bun, and Deno only. On the portable build (Cloudflare Workers, the browser, and edge runtimes) these APIs warn and no-op by default. Bind an environment source instead. See Compatibility.

Which files load

By default envapt loads .env, and in a detected environment it layers a per-environment cascade (covered in Environment).

Set envPaths to load a fixed, ordered list instead. Earlier paths win.

. = ['config/app.env', '.env'];
DANGER

Every path you list must exist. Setting envPaths validates the paths immediately, and a missing file throws EnvaptError with code EnvFilesNotFound. Setting envPaths also takes precedence: it disables the per-environment auto-cascade and any configureProfiles paths. Relative paths resolve against process.cwd() unless you set Envapter.baseDir (below).

envFileOptions tunes how files are read. override (default false) decides whether a later file or a file value can replace an already-set key; encoding (default utf8) sets the file encoding.

. = { : true, : 'utf8' };

Reading from a fixed directory

Relative paths resolve against the working directory (process.cwd()) by default. In a monorepo the process often starts at the repository root, not the package directory, so a package-local .env is not found. Set Envapter.baseDir to the directory that relative paths resolve against: the auto-cascade, configureProfiles paths, and relative envPaths. Absolute paths are used unchanged.

// ESM: resolve .env next to this module, whatever the working directory is
. = import.meta.;

// CommonJS
. = ;

baseDir accepts a directory path, a module URL (import.meta.url), or import.meta.dirname / __dirname. Set it before envPaths so relative envPaths validate against the same directory. Leave it unset to keep process.cwd() resolution.

Mirroring to process.env

By default a loaded value stays in envapt's cache. Set syncProcessEnv to mirror the keys envapt loaded back into process.env, so other libraries that read process.env directly get them.

. = true;

Only keys envapt's loader wrote are mirrored, so collisions follow envFileOptions.override: with the default false, a pre-existing process.env value is preserved.

Mirrored values are resolved first, so ${VAR} templates are expanded before they reach process.env and match what Envapter.get returns.

WARNING

Mirroring is one-way. Turning syncProcessEnv back to false does not remove keys already written; they stay in process.env until the process exits.

Debug logging

debug controls how much envapt writes to stderr (prefixed [envapt]). The levels are silent (default), warn, and verbose.

. = 'verbose';
  • warn: signals that may indicate misconfiguration: failed file reads, unresolved templates in non-strict mode, and any read of a missing or empty variable, or under strict mode a whitespace-only one (whether it returns a fallback or undefined).
  • verbose: adds the base directory, every loaded file, per-file key counts, per-key load lines, effective paths, cache rebuilds, and any present value a built-in converter could not parse (a malformed value that fell back to its default).

You can also set the level without code through the ENVAPT_DEBUG environment variable. envapt reads it on first access; an explicit Envapter.debug assignment overrides it.

ENVAPT_DEBUG=verbose
NOTE

import 'envapt/config' loads at import time. Set ENVAPT_DEBUG=verbose before the import to see that load logged. An Envapter.debug assignment in code runs after the load, so it is too late to log it.

On this page