Appearance
📄 getEnv
A lightweight utility for retrieving environment variables in Deno, supporting both local .env files (for development) and system environment variables (for production).
✨ Features
✅ Loads .env variables using deno_std/dotenv ✅ Falls back to Deno.env.get() for system environment ✅ Supports default fallback values ✅ Safe to use even if .env is missing (no crashes) ✅ Designed for Deno projects
📦 Installation
ts
import getEnv from './get-env.ts';🔧 How it works
Priority order: 1️⃣
.envfile (loaded at startup, dev mode) 2️⃣Deno.env.get()(system environment, prod mode) 3️⃣ Supplied default value (if provided) 4️⃣undefined(if not found anywhere)Uses the Deno standard library’s
dotenvmodule to parse.env.Automatically ignores
MissingEnvVarsErrorif no.envfile exists.
🚀 Usage
ts
const port = getEnv('PORT', '3000');
console.log(`Server running on port ${port}`);Without default:
ts
const token = getEnv('API_TOKEN');
if (!token) throw new Error('Missing API_TOKEN!');🔍 Example
Given a .env file:
PORT=8080
MODE=developmentCode:
ts
const port = getEnv('PORT', '3000'); // ➔ '8080'
const mode = getEnv('MODE'); // ➔ 'development'
const missing = getEnv('UNKNOWN', 'default'); // ➔ 'default'In production (without .env), it checks system environment:
bash
$ DENO_ENV=production deno run app.ts⚠️ Notes
- Only string values are supported.
undefinedis returned if the key is missing and no default is provided.- This module does not mutate
Deno.env— it only reads. - For secure environments, avoid hardcoding sensitive values.
Bien sûr ! Voici un exemple plus simple et clair d’utilisation avancée avec l’opérateur diamant (<T>) :
💎 Advanced Example with Type Parameter
ts
import getEnv from './get-env.ts';
// Define allowed keys using a union type
type MyEnvKeys = 'APP_NAME' | 'APP_PORT' | 'ENV';
// Use the diamond operator <MyEnvKeys> to hint TypeScript
const appName = getEnv<MyEnvKeys>('APP_NAME', 'DefaultApp');
const port = getEnv<MyEnvKeys>('APP_PORT', '8000');
const environment = getEnv<MyEnvKeys>('ENV', 'development');
console.log(`🚀 ${appName} | Port: ${port} | Mode: ${environment}`);What this does
✅ Type safety → You can’t accidentally pass a wrong key. ✅ Better autocomplete → Your editor will suggest only 'APP_NAME', 'APP_PORT', or 'ENV'. ✅ Clean fallback → Defaults are still supported.
Example Output
If .env contains:
APP_NAME=MyAdvancedApp
APP_PORT=4000
ENV=productionYou’ll get:
🚀 MyAdvancedApp | Port: 4000 | Mode: productionIf some keys are missing:
🚀 DefaultApp | Port: 8000 | Mode: developmentWhy use <MyEnvKeys>?
✅ Helps document which environment variables your app uses. ✅ Reduces typos and runtime errors. ✅ Makes your code self-explanatory.