Socket connection timeout

I have a mongodb database that I am connecting to with mongoose in nodejs. When I try connect to the serverless instance, it will normally work. However, I keep getting Socket connection timeout. If I manage to connect, once I refresh/restart the server, I get the socket error again. I am struggling to traceback the error to see what is causing it… Can anyone help?

I have also tried to connect using the mongodb nodejs package instead to see if it was an error with mongoose. Unfortunately, I get the same error still.


const { MongoClient, ServerApiVersion } = require('mongodb');

module.exports = async () => {

	// Create a MongoClient with a MongoClientOptions object to set the Stable API version
	const client = new MongoClient(process.env.SERVERLESS_DB_URI, {
	  serverApi: {
	    version: ServerApiVersion.v1,
	    strict: true,
	    deprecationErrors: true,

	try {
		// Connect the client to the server	(optional starting in v4.7)
		await client.connect();
		// Send a ping to confirm a successful connection
		await client.db("admin").command({ ping: 1 });
		console.log("Pinged your deployment. You successfully connected to MongoDB!");
	} finally {
		// Ensures that the client will close when you finish/error
		await client.close();


MongoNetworkError: Socket connection timeout
    at connectionFailureError (/Users/me/Desktop/app/server/node_modules/mongodb/lib/cmap/connect.js:370:20)
    at TLSSocket.<anonymous> (/Users/me/Desktop/app/server/node_modules/mongodb/lib/cmap/connect.js:293:22)
    at Object.onceWrapper (node:events:626:26)
    at TLSSocket.emit (node:events:511:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  cause: Error [ERR_SOCKET_CONNECTION_TIMEOUT]: Socket connection timeout
      at new NodeError (node:internal/errors:399:5)
      at internalConnectMultiple (node:net:1099:20)
      at Timeout.internalConnectMultipleTimeout (node:net:1638:3)
      at listOnTimeout (node:internal/timers:575:11)
      at process.processTimers (node:internal/timers:514:7) {
  connectionGeneration: 0,
  [Symbol(errorLabels)]: Set(0) {}