Skip to content

requireEnv

The requireEnv function returns the value of a required environment variable, throwing an error if it is not defined.

Syntax

typescript
requireEnv(key: string): string;

Parameters

NameTypeDescription
keystringThe name of the required environment variable

Returns

TypeDescription
stringThe value of the environment variable, if defined

Examples

typescript
requireEnv("NODE_ENV");
// => "development" (or another defined value)

requireEnv("NON_EXISTENT_VAR");
// => throws Error: Expected a valid value for 'key'

Notes

  • Compatible with Node.js and Deno environments.
  • Throws an error if the variable is not defined.

References

Released under the MIT License.