@tsirysndr/env-js@0.1.3Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
tsirysndr/env-jsGet environment variables in Deno, Bun and Node.js
This package works with Node.js, Deno, Bun
JSR Score
100%
Published
7 months ago (0.1.3)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677// deno-lint-ignore-file no-var no-explicit-any declare var Bun: any; declare var process: any; /** * Get the environment variable value * * @example * ```ts * import * as env from "jsr:@tsirysndr/env-js"; * console.log(env.get("HOME")); // /home/user */ export function get(key: string): string | undefined { if (Object.keys(globalThis).includes("Bun")) { return Bun.env[key]; } if (Object.keys(globalThis).includes("process")) { return process.env[key]; } if (Object.keys(globalThis).includes("Deno")) { return Deno.env.get(key); } return undefined; } /** * Verify if the environment variable exists * * @example * ```ts * import * as env from "jsr:@tsirysndr/env-js"; * console.log(env.has("HOME")); // true * ``` */ export function has(key: string): boolean { if (Object.keys(globalThis).includes("Bun")) { return !!Bun.env[key]; } if (Object.keys(globalThis).includes("process")) { return !!process.env[key]; } if (Object.keys(globalThis).includes("Deno")) { return Deno.env.has(key); } return false; } /** * Convert the environment variables to an object * * @example * ```ts * import * as env from "jsr:@tsirysndr/env-js"; * console.log(env.toObject()); * ``` */ export function toObject(): Record<string, string> { if (Object.keys(globalThis).includes("Bun")) { return Bun.env; } if (Object.keys(globalThis).includes("process")) { return process.env; } if (Object.keys(globalThis).includes("Deno")) { return Deno.env.toObject(); } return {}; }