Backend Environment Variables
Create a.env file in the backend directory with the following variables:
Server Configuration
The port number on which the backend server will run.Example:
PORT=5000The environment mode for the application. Use
development for local development and production for production deployments.Accepted values: development, production, testExample: NODE_ENV=developmentDatabase Configuration
The MongoDB connection string. Can be a local MongoDB instance or MongoDB Atlas cloud database.Local example:
MONGODB_URI=mongodb://localhost:27017/smartshelfAtlas example: MONGODB_URI=mongodb+srv://username:[email protected]/smartshelfJWT Configuration
Secret key used to sign and verify JSON Web Tokens for authentication.Example:
JWT_SECRET=your_super_secret_jwt_key_here_minimum_32_charactersGenerate a secure random string using:
The expiration time for JWT tokens. Uses time string format.Examples:
24h- 24 hours7d- 7 days60m- 60 minutes
JWT_EXPIRE=24hCORS Configuration
Comma-separated list of allowed origins for CORS (Cross-Origin Resource Sharing). Include all URLs where your frontend will be hosted.Example:
ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3000Production example: ALLOWED_ORIGINS=https://smartshelf.example.com,https://www.smartshelf.example.comBackend .env Example
Frontend Environment Variables
Create a.env file in the frontend directory with the following variables:
API Configuration
The base URL for the backend API. Must point to your backend server’s API endpoint.Development example:
VITE_API_URL=http://localhost:5000/apiProduction example: VITE_API_URL=https://api.smartshelf.example.com/apiAll environment variables in Vite must be prefixed with
VITE_ to be accessible in the application.Frontend .env Example
Security Best Practices
Rotate secrets regularly
Periodically update your
JWT_SECRET and other sensitive values, especially if there’s any possibility of compromise.Troubleshooting
Changes not taking effect
If you modify environment variables and changes aren’t reflected:- Restart the development server - Environment variables are loaded at startup
- Clear build cache - For Vite, delete the
node_modules/.vitecache directory - Verify file location - Ensure
.envis in the correct directory (backend or frontend root)
Connection errors
If you’re experiencing connection issues:- Backend: Verify
MONGODB_URIis correct and MongoDB is running - Frontend: Ensure
VITE_API_URLmatches your backend server URL and port - CORS errors: Add all frontend URLs to
ALLOWED_ORIGINS
Need help with deployment? See the Deployment Guide for production configuration best practices.