Last active
June 15, 2025 12:23
-
-
Save ilomon10/856cdae156f14208962326fd14917537 to your computer and use it in GitHub Desktop.
load environment file like nextjs, it can load multiple .env file and can replace in multiple stage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Deno | |
import { loadSync } from "jsr:@std/dotenv"; | |
import { log } from "./logging.ts"; | |
const loadMatrix = async (paths: string[]): Promise<Record<string, string>> => { | |
let result: Record<string, string> = {}; | |
const loadedFrom: string[] = []; | |
for (const path of paths) { | |
const res = loadSync({ | |
envPath: path, | |
}); | |
if (Object.keys(res).length > 0) { | |
loadedFrom.push(path); | |
} | |
result = { | |
...result, | |
...res, | |
}; | |
} | |
for (const [key, value] of Object.entries(result)) { | |
Deno.env.set(key, value); | |
} | |
log.info(`Loaded env from: ${loadedFrom.join(", ")}`); | |
return result; | |
}; | |
await loadMatrix([".env", ".env.local", ".env.production.local"]); | |
// then import on `main.ts` file | |
// import "load-env.deno.ts"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Node Javascript | |
import { configDotenv } from 'dotenv' | |
const loadMatrix = async (paths) => { | |
let result = {} | |
const loadedFrom = [] | |
for (const path of paths) { | |
const res = configDotenv({ | |
debug: true, | |
path: path, | |
encoding: 'utf-8', | |
}) | |
if (!res.error) { | |
loadedFrom.push(path) | |
} | |
result = { | |
...result, | |
...res, | |
} | |
} | |
console.info(`Loaded env from: ${loadedFrom.join(', ')}`) | |
return result | |
} | |
await loadMatrix(['.env', '.env.local', '.env.production.local']) | |
// then import on `index.js` file | |
// import "load-env.js" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Node Typescript | |
import { configDotenv } from 'dotenv'; | |
const loadMatrix = async (paths: string[]): Promise<Record<string, string>> => { | |
let result: Record<string, string> = {}; | |
const loadedFrom: string[] = []; | |
for (const path of paths) { | |
const res = configDotenv({ | |
debug: true, | |
path: path, | |
encoding: 'utf-8', | |
}); | |
if (!res.error) { | |
loadedFrom.push(path); | |
}; | |
result = { | |
...result, | |
...res, | |
}; | |
} | |
console.info(`Loaded env from: ${loadedFrom.join(', ')}`); | |
return result; | |
} | |
await loadMatrix(['.env', '.env.local', '.env.production.local']) | |
// then import on `index.ts` file | |
// import "load-env.ts" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment