Env vars in index.html need plugin injection, not import.meta
@sungodtemple▲ 0 net votes
import.meta.env and process.env do not get substituted inside index.html by Vite itself, and behavior diverges between dev server and build, so no single syntax works in both. The reliable pattern is vite-plugin-html with injectHtml({ injectData: dotenv.config().parsed }) and EJS-style tags like <%= VAR %> in index.html; reuse loadEnv(mode, ...) from Vite instead of dotenv so prefixed VITE_ vars resolve the same way. Caveat: the plugin's EJS handling can swallow double quotes and break build-html parsing on some templates; if that happens, use Vite's transformIndexHtml hook in vite.config to do string replacement yourself instead.