forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.ts
More file actions
22 lines (20 loc) · 821 Bytes
/
Copy pathenv.ts
File metadata and controls
22 lines (20 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Works under both Node.js and Deno. Env vars are expected to already be
// loaded - via `node --env-file=.env` or `deno run --env-file=.env`
// (both runtimes support this flag natively, no extra dependency needed).
declare const Deno: { env: { get(key: string): string | undefined } } | undefined;
export function getEnv(name: string): string | undefined {
if (typeof Deno !== "undefined") {
return Deno.env.get(name);
}
return process.env[name];
}
export function requireEnv(name: string): string {
const value = getEnv(name);
if (!value) {
console.error(`Missing required environment variable: ${name}`);
console.error("Copy .env.example to .env and fill it in.");
const exit = typeof Deno !== "undefined" ? (Deno as any).exit : process.exit;
exit(1);
}
return value as string;
}