diff --git a/packages/vite/src/node/env.ts b/packages/vite/src/node/env.ts index 10e570713188da..431409c2d8b942 100644 --- a/packages/vite/src/node/env.ts +++ b/packages/vite/src/node/env.ts @@ -35,15 +35,29 @@ export function loadEnv( }), ) - // let environment variables use each other - const expandParsed = expand({ + const expandOptions = { parsed: { ...(process.env as any), ...parsed, }, // prevent process.env mutation ignoreProcessEnv: true, - }).parsed! + } + + let expandParsed: NonNullable['parsed']> + try { + // let environment variables use each other + expandParsed = expand(expandOptions).parsed! + } catch (e) { + // custom error handling until https://github.com/motdotla/dotenv-expand/issues/65 is fixed upstream + // check for message "TypeError: Cannot read properties of undefined (reading 'split')" + if (e.message.includes('split')) { + throw new Error( + 'dotenv-expand failed to expand env vars. Maybe you need to escape `$`?', + ) + } + throw e + } Object.keys(parsed).forEach((key) => { parsed[key] = expandParsed[key]