Hyperlinkv0.8.0-beta.28

ConfigProvider

ConfigProvider.fromEnvfunctioneffect/ConfigProvider.ts:848
(options?: {
  readonly env?: Record<string, string> | undefined
  readonly preserveEmptyStrings?: boolean | undefined
}): ConfigProvider

Creates a ConfigProvider backed by environment variables.

When to use

Use to read configuration from process.env, which is the default when no provider is explicitly set, or pass a custom env record for testing or non-Node runtimes.

Details

Path segments are joined with _ for direct lookup, and env var names are also split on _ to build a trie for child key discovery. This means DATABASE_HOST=localhost is accessible at both path ["DATABASE_HOST"] and ["DATABASE", "HOST"]. If all immediate children of a trie node have purely numeric names, the node is reported as an Array; otherwise as a Record.

The default environment merges process.env and import.meta.env (when available). Override by passing { env: { ... } }.

Literal empty strings are treated as missing values when loaded as values by default. Pass { preserveEmptyStrings: true } to keep empty strings as explicit values. Child discovery still reflects the environment variable names present in the source.

Never fails with SourceError — all lookups are synchronous.

Example (Reading from a custom env record)

import { Config, ConfigProvider, Effect } from "effect"

const provider = ConfigProvider.fromEnv({
  env: {
    DATABASE_HOST: "localhost",
    DATABASE_PORT: "5432"
  }
})

const host = Config.string("HOST").parse(
  provider.pipe(ConfigProvider.nested("DATABASE"))
)

// Effect.runSync(host) // "localhost"
export function fromEnv(options?: {
  readonly env?: Record<string, string> | undefined
  readonly preserveEmptyStrings?: boolean | undefined
}): ConfigProvider {
  const env: Record<string, string | undefined> = options?.env ?? {
    ...globalThis?.process?.env,
    ...(import.meta as any)?.env
  }
  const preserveEmptyStrings = options?.preserveEmptyStrings === true
  const trie = buildEnvTrie(env)

  return make((path) => Effect.succeed(nodeAtEnv(trie, env, path, preserveEmptyStrings)))
}
Referenced by 3 symbols