Hyperlinkv0.8.0-beta.28

ConfigProvider

ConfigProvider.fromDotEnvconsteffect/ConfigProvider.ts:1110
(options?: {
  readonly path?: string | undefined
  readonly expandVariables?: boolean | undefined
  readonly preserveEmptyStrings?: boolean | undefined
}): Effect.Effect<ConfigProvider, PlatformError, FileSystem.FileSystem>

Creates a ConfigProvider by reading and parsing a .env file from the file system.

When to use

Use to load environment config from a .env file at application startup.

Details

Requires FileSystem in the Effect context. Defaults to reading ".env" in the current directory; override with { path: "/custom/.env" }. Variable expansion (for example, ${VAR}) is disabled by default; enable with { expandVariables: true }.

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 keys present in the parsed .env source.

Returns an Effect that resolves to a ConfigProvider. Fails with a PlatformError if the file cannot be read.

Example (Loading a .env file)

import { ConfigProvider, Effect } from "effect"

const program = Effect.gen(function*() {
  const provider = yield* ConfigProvider.fromDotEnv()
  return provider
})
export const fromDotEnv: (options?: {
  readonly path?: string | undefined
  readonly expandVariables?: boolean | undefined
  readonly preserveEmptyStrings?: boolean | undefined
}) => Effect.Effect<ConfigProvider, PlatformError, FileSystem.FileSystem> = Effect.fnUntraced(
  function*(options) {
    const fs = yield* FileSystem.FileSystem
    const content = yield* fs.readFileString(options?.path ?? ".env")
    return fromDotEnvContents(content, options)
  }
)