There's a chicken-egg problem here: Vite expects to resolve .env files from project root, but project root can be made different by the config file.
So if we resolve .env before resolving the config file, we can only resolve it from CWD, which would then break the case where the user puts .env files in a nested root specified via config.
摘自 Evan You 的回复今天在配置vite.config的时候使用到import.meta.env来设置项目路径,本打算直接可以想vue2一样使用,执行npm run build的时候却发现报错了,不能这样用,这就很奇怪了,而且现在3.0和vite还没有广泛使用,查文档不是很清楚。所以就想到了github的issues,果然有和我一样需求的人问了这个问题。
// dotenv 需要单独npm install export default ({ mode }) => { require('dotenv').config({ path: `./.env.${mode}` }); // now you can access config with process.env.{configName} return defineConfig({ plugins: [vue()], base:process.env.VITE_APP_NAMEimport { loadEnv } from 'vite' export default ({ mode }) => { return defineConfig({ plugins: [vue()],