# tsconfig.json { "compilerOptions": { "types": ["vite/client"], ... } }
讀取環境變量文件.env Files
Vite使用dotenv從你的項目根目錄下的以下文件中加載額外的環境變量:
.env.[mode] # only loaded in specified mode
為了防止意外地將env變量泄漏到客戶端,只有以VITE_前綴的變量才會暴露給經過VITE_處理的代碼。例如:
# .env.production # 讀取 console.log(import.meta.env.VITE_API_URL); # 讀取 console.log(import.meta.env.MAP_KEY); MAP_KEY = xxxxxxxxxxxxxxx VITE_API_URL = http://prod.api.com
只有VITE_API_URL會暴露為import.meta.env。您的客戶端源代碼的VITE_API_URL,但MAP_KEY不會。