Skip to content

getEnv

The getEnv function returns the value of an environment variable, with support for a fallback if it does not exist.

Syntax

typescript
getEnv(key: string, fallback?: string): string | undefined;

Parameters

NameTypeDescription
keystringThe name of the environment variable to retrieve
fallbackstringAlternative value if the variable does not exist (optional)

Returns

TypeDescription
string | undefinedThe value of the environment variable, or the fallback, or undefined if neither exists

Examples

typescript
getEnv("NODE_ENV");
// => "production", "development", etc.

getEnv("NON_EXISTENT", "defaultValue");
// => "defaultValue"

Notes

  • Supports Node.js and Deno environments.
  • Returns the fallback value if the variable does not exist.

References

Released under the MIT License.