Mastering Environment Variables in Vite
0. Offical Doc
https://vite.dev/guide/env-and-mode
Variable Naming Convention
Vite has a specific convention for exposing variables to your client-side code:
- Only variables prefixed with
VITE_
are exposed to your Vite-processed code - This is a security feature to prevent accidentally exposing sensitive variables
1. Practical Implementation
1. Creating Environment Files
First, create an .env
file in your project root:
1 |
|
2. Accessing Environment Variables in Your Code
Now you can use these variables in your application:
1 |
|
3. Type Definitions for TypeScript Projects
If you’re using TypeScript, you’ll want proper type definitions. Create a env.d.ts
file:
1 |
|
Mastering Environment Variables in Vite
https://www.hardyhu.cn/2025/02/20/Mastering-Environment-Variables-in-Vite/