Environment Variables in Bun

From the Bun cheat sheet ยท Environment & Utilities ยท verified Jul 2026

Environment Variables

Access and manage environment variables with auto .env loading

typescript
// Access env vars (auto-loaded from .env)
const dbUrl = Bun.env.DATABASE_URL;
const port = Bun.env.PORT || "3000";

// Also available on process.env
const key = process.env.API_KEY;
๐Ÿ’ก Bun auto-loads .env files with no dotenv package โ€” just create the file and go
โšก Use bun --env-file to load a specific env file for different environments
๐Ÿ“Œ Both Bun.env and process.env work โ€” Bun.env is slightly faster as it skips Node compat
๐ŸŸข import.meta.dir/file/path give you the current file location โ€” no __dirname polyfill needed
envenvironmentdotenv

More Bun tasks

Back to the full Bun cheat sheet