React + Vite + TypeScript 读取环境变量报错: Uncaught ReferenceError: process is not defined 作者: Chuwen 时间: 2022-01-05 分类: React Uncaught ReferenceError: process is not defined: 相关来源:https://github.com/vitejs/vite/issues/1973#issuecomment-787571499 解决办法,在 `vite.config.json`: ```json import {defineConfig, loadEnv} from 'vite' import react from '@vitejs/plugin-react' export default ({mode}: { mode: string }) => { return defineConfig({ define:{ 'process.env':{...process.env, ...loadEnv(mode, process.cwd())} } }); } ``` 然后在你的环境变量文件以 `VITE_` 开头命名,然后使用 `process.env.VITE_XXX` 获取就好了 标签: React