A validated EnvVars object.
The function looks for a .env file in the project root directory (two levels
above this file). If the file exists, its variables are parsed and take precedence
over any pre‑existing environment variables. Then it reads from Bun.env
(or falls back to process.env when not in Bun).
The following validation is performed:
BASE_URL must be present and be a valid HTTP/HTTPS URL.LOGIN_PATH and LOGOUT_PATH cannot be empty strings and must not contain ? or #.USER_EMAIL must be a valid email format if provided and non‑empty.USER_PASSWORD cannot be an empty string if provided.// .env file contains:
// BASE_URL=http://localhost:3000
// USER_EMAIL=admin@example.com
const env = loadEnv();
console.log(env.BASE_URL); // "http://localhost:3000"
console.log(env.USER_EMAIL); // "admin@example.com"
Loads environment variables from the project's
.envfile andBun.env.