Browse API

API · v0.32.0 · changelog

env

Standard library environment variables

Runtime support

  • Node
  • Deno
  • Bun
  • Browser

Browser: Uses an empty environment. has() returns false, and get() requires a fallback.

Member signatures use schematic object.member(...) API notation. Exported type declarations are valid TypeScript.

Read-only access to the merged runtime environment.

Node and Bun use process.env; Deno uses Deno.env. The first matching dotenv file is layered over that native environment. Browser imports use an empty environment and never include server variables in client code.

ts
import env from "@rcompat/env";

env.get#

Returns the environment variable or the supplied fallback.

Parameters

  • key — Environment variable name.
  • fallback — Value to return when the variable is missing.

Throws: Error when both the variable and fallback are undefined.

ts
env.get<F extends NonNullable<unknown> | null = never>(
    key: string, fallback?: F,
  ): string | F;

env.has#

Checks whether an environment variable is defined.

Parameters

  • key — Environment variable name.
ts
env.has(key: string): boolean;

env.toJSON#

Returns a detached, mutable snapshot with a null prototype.

Returns: All loaded environment variables as a new dictionary.

ts
env.toJSON(): Dict<string>;