loadEnv() — vite Function Reference
Architecture documentation for the loadEnv() function in env.ts from the vite codebase.
Entity Profile
Dependency Diagram
graph TD dbbbbbec_1500_ac8a_4605_9194e2cfb45d["loadEnv()"] 7fa76fc1_cb1b_cf98_0900_1217276f6616["env.ts"] dbbbbbec_1500_ac8a_4605_9194e2cfb45d -->|defined in| 7fa76fc1_cb1b_cf98_0900_1217276f6616 58c4a210_68fe_1b4d_ed49_d59203f97ef1["resolveConfig()"] 58c4a210_68fe_1b4d_ed49_d59203f97ef1 -->|calls| dbbbbbec_1500_ac8a_4605_9194e2cfb45d 19ce2051_6a74_4b8b_104d_ec006cd7075f["arraify()"] dbbbbbec_1500_ac8a_4605_9194e2cfb45d -->|calls| 19ce2051_6a74_4b8b_104d_ec006cd7075f ff7562f3_f396_12b1_2d0c_c03c1ee28f2c["getEnvFilesForMode()"] dbbbbbec_1500_ac8a_4605_9194e2cfb45d -->|calls| ff7562f3_f396_12b1_2d0c_c03c1ee28f2c a9bd45ce_8339_2b77_7543_41c306ebdb02["tryStatSync()"] dbbbbbec_1500_ac8a_4605_9194e2cfb45d -->|calls| a9bd45ce_8339_2b77_7543_41c306ebdb02 style dbbbbbec_1500_ac8a_4605_9194e2cfb45d fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/vite/src/node/env.ts lines 27–95
export function loadEnv(
mode: string,
envDir: string | false,
prefixes: string | string[] = 'VITE_',
): Record<string, string> {
const start = performance.now()
const getTime = () => `${(performance.now() - start).toFixed(2)}ms`
if (mode === 'local') {
throw new Error(
`"local" cannot be used as a mode name because it conflicts with ` +
`the .local postfix for .env files.`,
)
}
prefixes = arraify(prefixes)
const env: Record<string, string> = {}
const envFiles = getEnvFilesForMode(mode, envDir)
debug?.(`loading env files: %O`, envFiles)
const parsed = Object.fromEntries(
envFiles.flatMap((filePath) => {
const stat = tryStatSync(filePath)
// Support FIFOs (named pipes) for apps like 1Password
if (!stat || (!stat.isFile() && !stat.isFIFO())) return []
const parsedEnv = parseEnv(fs.readFileSync(filePath, 'utf-8'))
return Object.entries(parsedEnv as Record<string, string>)
}),
)
debug?.(`env files loaded in ${getTime()}`)
// test NODE_ENV override before expand as otherwise process.env.NODE_ENV would override this
if (parsed.NODE_ENV && process.env.VITE_USER_NODE_ENV === undefined) {
process.env.VITE_USER_NODE_ENV = parsed.NODE_ENV
}
// support BROWSER and BROWSER_ARGS env variables
if (parsed.BROWSER && process.env.BROWSER === undefined) {
process.env.BROWSER = parsed.BROWSER
}
if (parsed.BROWSER_ARGS && process.env.BROWSER_ARGS === undefined) {
process.env.BROWSER_ARGS = parsed.BROWSER_ARGS
}
// let environment variables use each other. make a copy of `process.env` so that `dotenv-expand`
// doesn't re-assign the expanded values to the global `process.env`.
const processEnv = { ...process.env } as DotenvPopulateInput
expand({ parsed, processEnv })
// only keys that start with prefix are exposed to client
for (const [key, value] of Object.entries(parsed)) {
if (prefixes.some((prefix) => key.startsWith(prefix))) {
env[key] = value
}
}
// check if there are actual env variables starting with VITE_*
// these are typically provided inline and should be prioritized
for (const key in process.env) {
if (prefixes.some((prefix) => key.startsWith(prefix))) {
env[key] = process.env[key] as string
}
}
debug?.(`using resolved env: %O`, env)
return env
}
Domain
Subdomains
Defined In
Called By
Source
Frequently Asked Questions
What does loadEnv() do?
loadEnv() is a function in the vite codebase, defined in packages/vite/src/node/env.ts.
Where is loadEnv() defined?
loadEnv() is defined in packages/vite/src/node/env.ts at line 27.
What does loadEnv() call?
loadEnv() calls 3 function(s): arraify, getEnvFilesForMode, tryStatSync.
What calls loadEnv()?
loadEnv() is called by 1 function(s): resolveConfig.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free