mongodb-memory-server: MongoMemoryReplSet keeps hanging indefinitely - MongoServerSelectionError: Server selection timed out after 30000 ms

Versions

  • NodeJS: 14.15.1
  • mongodb-memory-server-*: 6.9.6(also tried with 7.0.0-beta.26)
  • mongodb: 0.0.0 // default
  • mongoose: 5.11.15
  • system: MacOS Big Sur 11.1(intel)

package: mongo-memory-server

What is the Problem?

I get the following error. MongoServerSelectionError: Server selection timed out after 30000 ms. According to the logs, the program is stuck at await replSet.waitUntilRunning). I have tried single instance mongo and it works fine.

Code Example

let replSet: any
beforeAll(async () => {
  console.log('beforeAll')
  const replSet = new MongoMemoryReplSet({
    replSet: { storageEngine: 'wiredTiger' },
  })

  await replSet.waitUntilRunning() //its stuck here!!
  console.log("replica set ready")
  const uri = await replSet.getUri()
  console.log({ uri })
  await mongoose.connect(uri, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    useCreateIndex: true,
  })
  console.log('mongodb connected')
})

Do you know why it happenes?

No, I had this same setup few months ago in another laptop and it worked fined. I wonder if it has to do with macOS Big Sur

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18

Most upvoted comments

I ran into the same issue (Server selection timed out after 30000 ms) on macOS High Sierra with 7.0.0-beta.26, node 14.15.3 After wasting 4 hours, I finally work it out. Just

  1. remove node_modules
  2. remove yarn.lock
  3. reinstall node_modules

Done Hope this will help you

.create is in typescript, because this project is written in typescript & export with full types, but .create is an new function in 7.0.0 - before it was an auto-start on new Class() which could not be disabled