This document explains how environment variables work in this Chrome extension project.
The project automatically detects the environment based on how it's running:
- Triggered by:
npm run dev - Environment:
import.meta.env.MODE = "development" - Storage: Uses
localStoragewith prefixed keys - Demo: Shows Storage Demo component
- Triggered by:
npm run buildornpm run build:extension - Environment:
import.meta.env.MODE = "production" - Storage: Uses
chrome.storageAPI - Demo: Shows Copilot Demo component
- Triggered by: Running as loaded Chrome extension
- Detection:
chrome.runtime.idexists - Storage: Uses
chrome.storageAPI
Create a .env file in the project root with these variables:
# App Configuration
VITE_APP_NAME=Him
VITE_APP_VERSION=1.0.0
# API Configuration
VITE_API_BASE_URL=http://localhost:3001
VITE_DEFAULT_MODEL=gemini-2.5-flash
# Development Settings
VITE_ENABLE_STORAGE_DEMO=true
VITE_DEBUG_MODE=true-
Copy the example file:
cp env.example .env
-
Modify the values as needed
-
Restart the dev server:
npm run dev
chrome-extension/
├── .env # Environment variables (create this)
├── env.example # Example environment file
├── src/
│ ├── utils/
│ │ └── environment.js # Environment detection utility
│ ├── hooks/
│ │ └── useChromeStorage.js # Storage hook
│ └── App.jsx # Main app component
└── vite.config.js # Vite configuration
import { getConfig, isDevelopment, isChromeExtension } from '../utils/environment';
const MyComponent = () => {
const config = getConfig();
if (config.isDev) {
console.log('Running in development mode');
}
if (config.isExtension) {
console.log('Running as Chrome extension');
}
};import useChromeStorage from '../hooks/useChromeStorage';
const MyComponent = () => {
// Automatically uses localStorage in dev, chrome.storage in extension
const [apiKey, setApiKey] = useChromeStorage('apiKey', '', STORAGE_TYPES.SYNC);
};Open browser console and look for:
🌍 Environment Configuration: {
mode: "development",
isDevelopment: true,
isProduction: false,
isChromeExtension: false,
appName: "Him",
apiBaseUrl: "http://localhost:3001",
storagePrefix: "dev_"
}
In development mode, localStorage keys will be prefixed:
dev_local_apiKeydev_sync_themedev_session_tempData
true: Shows storage demo in developmentfalse: Shows copilot demo in development
true: Enables additional loggingfalse: Reduces console output
- Development:
http://localhost:3001 - Production: Your actual API endpoint
- Run
npm run build:extension - Load extension in Chrome
- Storage automatically switches to
chrome.storage
- Run
npm run dev - Open dev server URL
- Storage automatically switches to
localStorage
No code changes needed - the environment detection handles everything automatically!