This guide explains how to set up environment variables for your Vite React app.
Vite uses import.meta.env instead of process.env for environment variables.
Create a .env file in your project root:
# Gmail SMTP Configuration
VITE_GMAIL_USER=phcoder.blog@gmail.com
VITE_GMAIL_APP_PASSWORD=your-16-character-app-passwordUpdate your vercel.json:
{
"rewrites": [
{
"source": "/(.*)",
"destination": "/index.html"
}
],
"env": {
"VITE_GMAIL_USER": "phcoder.blog@gmail.com",
"VITE_GMAIL_APP_PASSWORD": "your-app-password"
}
}| React (Create React App) | Vite |
|---|---|
process.env.REACT_APP_* |
import.meta.env.VITE_* |
REACT_APP_GMAIL_USER |
VITE_GMAIL_USER |
REACT_APP_GMAIL_APP_PASSWORD |
VITE_GMAIL_APP_PASSWORD |
- Must start with
VITE_- Only variables with this prefix are exposed to your app - Case sensitive -
VITE_GMAIL_USERis different fromVITE_gmail_user - Restart dev server - After adding
.envfile, restart your development server
After setting up the environment variables:
-
Restart your development server:
npm run dev
-
Test newsletter subscription:
- Subscribe with a test email
- Check browser console for email logs
- Should see:
β Mock welcome email sent successfully (Development mode)
-
Test with Gmail SMTP (if configured):
- Should see:
β Email sent successfully via Gmail SMTP
- Should see:
π§ Sending welcome email to: user@example.com
π§ Using mock email service for welcome email...
β
Mock welcome email sent successfully (Development mode)
π§ Sending welcome email to: user@example.com
π§ Gmail SMTP - Sending email to: user@example.com
β
Email sent successfully via Gmail SMTP
- Check file imports - Make sure you're using
import.meta.env - Restart dev server - After adding
.envfile - Clear browser cache - Hard refresh the page
- Check variable names - Must start with
VITE_ - Check file location -
.envmust be in project root - Check syntax - No spaces around
=in.envfile
- Created
.envfile withVITE_prefix - Updated
vercel.jsonwith correct variable names - Restarted dev server after changes
- Tested newsletter subscription - No more errors
- Console shows email logs - Mock or real emails
Your email system should now work without the "process is not defined" error! π