A modern, full-stack e-commerce application built with Next.js, React, MongoDB, and TailwindCSS. Features product listings, shopping cart functionality, and responsive design.
- 🛍️ Product catalog with responsive grid layout
- 🛒 Shopping cart with localStorage persistence
- 📱 Mobile-first responsive design
- 🎨 Modern UI with TailwindCSS
- 🔍 Product detail pages
- 💾 MongoDB database integration
- Frontend: Next.js 14, React 18, TailwindCSS
- Backend: Next.js API Routes
- Database: MongoDB with Mongoose
- State Management: React Context API
Before you begin, ensure you have the following installed:
- Node.js (v18 or higher)
- npm or yarn
- MongoDB account (local or cloud like MongoDB Atlas)
git clone <your-repo-url>
cd Diligent-Ecomercenpm installCreate a .env.local file in the root directory:
MONGODB_URI=your_mongodb_connection_string_hereGetting your MongoDB URI:
-
MongoDB Atlas (Cloud):
- Create a free account at MongoDB Atlas
- Create a new cluster
- Click "Connect" → "Connect your application"
- Copy the connection string and replace
<password>with your database password - Add your database name at the end:
mongodb+srv://username:password@cluster.mongodb.net/diligent-ecommerce?retryWrites=true&w=majority
-
Local MongoDB:
MONGODB_URI=mongodb://localhost:27017/diligent-ecommerce
Populate your database with sample products:
npm run seedThis will insert 10 sample products into your MongoDB database. The script will:
- Clear existing products (optional - you can comment this out in
data/seed.jsif needed) - Insert sample products with images, prices, descriptions, and stock levels
npm run devOpen http://localhost:3000 in your browser to see the application.
npm run dev- Start the development servernpm run build- Build the application for productionnpm run start- Start the production servernpm run lint- Run ESLint to check code qualitynpm run seed- Seed the database with sample products
Diligent-Ecomerce/
├── components/ # Reusable React components
│ ├── Navbar.jsx # Navigation bar with cart icon
│ └── ProductCard.jsx # Product card component
├── context/ # React Context providers
│ └── CartContext.jsx # Shopping cart state management
├── data/ # Data scripts
│ └── seed.js # Database seeding script
├── lib/ # Utility functions
│ └── db.js # MongoDB connection
├── models/ # Mongoose models
│ └── Product.js # Product schema
├── pages/ # Next.js pages and API routes
│ ├── api/ # API endpoints
│ │ └── products/ # Product API routes
│ ├── cart.jsx # Shopping cart page
│ ├── index.jsx # Homepage
│ ├── product/ # Product detail pages
│ └── _app.js # Custom App component
├── styles/ # Global styles
│ └── globals.css # TailwindCSS directives
├── .env.local # Environment variables (create this)
├── package.json # Dependencies and scripts
├── tailwind.config.js # TailwindCSS configuration
└── postcss.config.js # PostCSS configuration
Vercel is the recommended platform for deploying Next.js applications. Follow these steps:
Ensure your code is pushed to a Git repository (GitHub, GitLab, or Bitbucket).
- Go to vercel.com
- Sign up or log in with your Git provider
- Click "Add New Project"
- Import your Git repository
- Vercel will automatically detect Next.js
- In the project settings, go to "Environment Variables"
- Add your
MONGODB_URI:- Name:
MONGODB_URI - Value: Your MongoDB connection string
- Environment: Production, Preview, and Development (select all)
- Name:
- Click "Deploy"
- Vercel will build and deploy your application
- Your app will be live at
https://your-project.vercel.app
After deployment, you can seed your production database:
-
Use Vercel's CLI to run the seed script:
npm i -g vercel vercel login vercel env pull .env.local npm run seed
Or use MongoDB Compass or MongoDB Atlas web interface to run the seed script manually.
- MongoDB Atlas: If using MongoDB Atlas, ensure your IP whitelist includes
0.0.0.0/0(all IPs) or add Vercel's IP ranges - Environment Variables: Always set environment variables in Vercel dashboard, never commit
.env.localto Git - Build Time: The build process will run
npm run buildautomatically - Serverless Functions: API routes will be deployed as serverless functions
- Verify your
MONGODB_URIis correct in.env.local - Check MongoDB Atlas IP whitelist (should include
0.0.0.0/0for Vercel) - Ensure your MongoDB username and password are URL-encoded if they contain special characters
- Make sure
.env.localexists and containsMONGODB_URI - Verify MongoDB connection is working:
npm run devand check if products load - Check that
dotenvpackage is installed:npm install dotenv --save-dev
- Run
npm run lintto check for code issues - Ensure all dependencies are installed:
npm install - Check Node.js version compatibility (v18+)
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is open source and available under the MIT License.