-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.js
40 lines (36 loc) · 896 Bytes
/
env.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { existsSync } from "node:fs";
/**
* Load environment variables from .env* files
* into process.env in the following order:
*
* 1. .env.<NODE_ENV>.local
* 2. .env.<NODE_ENV>
* 3. .env.local
* 4. .env
* 5. .env.defaults
*
* If a variable already exists in the environment,
* it will be not overwritten.
*/
export default function env() {
if (!process.loadEnvFile) {
console.warn(
"🌻 <process.loadEnvFile> is not available. Please ensure your environment is properly configured."
);
return;
}
const files = [".env.defaults", ".env", ".env.local"];
if (process.env.NODE_ENV) {
files.push(
`.env.${process.env.NODE_ENV}`,
`.env.${process.env.NODE_ENV}.local`
);
}
files
.toReversed()
.filter(existsSync)
.forEach((file) => {
console.info(`🌻 Loading ${file}`);
process.loadEnvFile(file);
});
}