I am getting an error in console, can you please help I am trying to run my express server. my goal is to be able to run the server.js.
import app from './server';
import mongodb, { MongoClient } from 'mongodb';
import dotenv from 'dotenv';
dotenv.config();
const MongoClient = mongodb.MongoClient;
const port = process.env.PORT || 8000;
MongoClient.connect(process.env.RESTREVIEWS_DB_URI, {
maxPoolSize: 50,
wtimeoutMS: 2500,
useNewUrlParser: true,
})
.catch((err) => {
console.error(err.stack);
process.exit(1);
})
.then(async (client) => {
app.listen(port, () => {
console.log(`listening on port ${port}`);
});
});
Then the console is showing after I run server.js
file:///C:/Users/jayde/Desktop/react/restaurant-reviews/backend/index.js:5 const MongoClient = mongodb.MongoClient;
SyntaxError: Identifier 'MongoClient' has already been declared at Loader.moduleStrategy (internal/modules/esm/translators.js:145:18) at async link (internal/modules/esm/module_job.js:64:21) [nodemon]
CodePudding user response:
As you are already importing MongoClient
here:
import mongodb, { MongoClient } from 'mongodb';
^^^^^^^^^^^
here it is
… it does not make sense to import it again here:
const MongoClient = mongodb.MongoClient;
^^^^^^^^^^^
remove this
CodePudding user response:
import mongodb, { MongoClient } from 'mongodb';
This line defines two constants: mongodb
and MongoClient
.
So it throws an error when you try to make another variable called MongoClient
.
const MongoClient = mongodb.MongoClient;
You should be able to remove that whole line and it should work.