backend-mongodb-setup
is an NPM package that helps set up MongoDB connection in a Node.js backend.
Run the following command inside your backend project:
npx backend-mongodb-setup
1️⃣ Creates a src/config/database.js
file.
2️⃣ Ensures .env
contains MongoDB connection string.
3️⃣ Connects MongoDB using Mongoose.
const mongoose = require('mongoose');
require('dotenv').config();
const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('✅ MongoDB Connected');
} catch (error) {
console.error('❌ MongoDB Connection Error:', error);
process.exit(1);
}
};
module.exports = connectDB;
If .env
already exists, it updates it without removing existing data.
MONGO_URI=mongodb://localhost:27017/mydatabase
✔ Handles MongoDB connection setup
✔ Automatically updates .env
file
✔ Uses Mongoose for easy database interactions