Sources

Bind an environment source with useSource, and what the Node, Worker, and manual providers read.

An environment source is the object envapt reads variables from. On Node, Bun, and Deno a source is bound for you on import, reading process.env and the .env file cascade. On Cloudflare Workers and in the browser there is none of that, so you bind a source yourself with Envapter.useSource(...) and read with the same typed API.

NOTE

On Node, Bun, and Deno you never call useSource. The Node entry binds a source that reads a clone of process.env plus the .env cascade at import time.

The providers

envapt ships three sources.

  • NodeEnvSource is bound automatically on the Node entry and is Node-only. It reads a clone of process.env and the .env cascade.
  • WorkerEnvSource wraps the Cloudflare env binding. See Workers.
  • ManualEnvSource wraps any object you pass, like the config your bundler injects into a browser build, so you can pass import.meta.env directly. See Browser.

WorkerEnvSource and ManualEnvSource both snapshot the object at construction (so later mutation does not leak in) and JSON-stringify non-string values so the converters parse them the same way.

You pass the source the object the runtime exposes, the Worker env binding or the bundler's injected config, not one you write out by hand. The value is the typed read on top:

declare const : <string, unknown>; // the Cloudflare env binding
.(new ());

const  = .('UPSTREAM_URL', .); // URL | undefined
const  = .('CACHE_TTL', ., '5m'); // number (ms)

Any object can be a source

EnvSource is one required method, readVars(): Record<string, string>. Any object that satisfies it works, so you can adapt another config system, a secrets payload or an RPC response, without writing a provider.

declare const : <string, string>; // fetched from your secrets manager
const :  = { : () =>  };
.();

const  = .('API_BASE_URL', .);
const  = .('DB_POOL_SIZE', 10);

An optional supportsFiles flag marks a source as backing the file-only config APIs. Only NodeEnvSource sets it, which is why the .env cascade and envPaths work on Node and nowhere else.

When nothing is bound

Reading a value before you call useSource throws EnvaptError with code NoSourceBound. It cannot happen on Node, where the source is bound on import.

The file-only config APIs (envPaths, baseDir, envFileOptions, configureProfiles, resetProfiles) need a source that backs the filesystem. Calling one on a bare source throws EnvaptError with code FileApiUnsupported. See Errors for both.

WARNING

Off Node there is no filesystem: the .env cascade and the file-only config APIs are absent. Pass your values through ManualEnvSource or WorkerEnvSource instead.

For the per-runtime flows see Workers and Browser; the build selection is in Compatibility.

On this page