try creating /db.js file with following connection function
const mongoose=require('mongoose')
async function connectMongodb(url){
return mongoose.connect(url)
.then(() => console.log(new Date().toISOString()+": Mongodb connected"))
.catch(err => console.log("error", err))
}
module.exports ={
connectMongodb,
}
and in your server.js/index.js file import and run funtion with suitable mongo uri in following way
const newLocal = `./db`;
const { connectMongodb } = require(newLocal);
//establishin db connections
connectMongodb(process.env.MongoUrl)
in .env add MongoUrl="mongodb://root:[email protected]:27017/NextBase?authSource={your collection name}"