Skip to content

Instantly share code, notes, and snippets.

@franky47
Created January 6, 2026 18:47
Show Gist options
  • Select an option

  • Save franky47/9ce6eaf80b4c74b63988370b7c39fd23 to your computer and use it in GitHub Desktop.

Select an option

Save franky47/9ce6eaf80b4c74b63988370b7c39fd23 to your computer and use it in GitHub Desktop.
loadEnv
import { loadEnvFile } from 'node:process'
export function loadEnv() {
const nodeEnv = process.env.NODE_ENV || 'development'
// Load in order of precedence
const files = [
`.env.${nodeEnv}.local`,
nodeEnv === 'test' ? null : `.env.local`,
`.env.${nodeEnv}`,
`.env`,
].filter((file): file is string => !!file)
for (const file of files) {
try {
loadEnvFile(file)
break // Stop after the first successful load
} catch {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment