I have problem with connecting to MongoDB locally after upgrading NodeJS version.
NodeJS - 16.17.0
Project MongoDB (package.json) - "mongodb": "^4.1.1"
MongoDB on system (Windows 10) - 6.0.3
Here is the error log:
MongoServerSelectionError: connection timed out
at Timeout._onTimeout (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\sdam\topology.ts:591:30)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined,
[Symbol(errorLabels)]: Set(0) {}
}
MongoTopologyClosedError: Topology is closed
at processWaitQueue (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\sdam\topology.ts:904:42)
at Topology.selectServer (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\sdam\topology.ts:601:5)
at Topology.<anonymous> (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\sdam\topology.ts:252:17)
at node:internal/util:361:7
at new Promise (<anonymous>)
at Topology.selectServerAsync (node:internal/util:347:12)
at executeOperationAsync (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\operations\execute_operation.ts:110:20)
at D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\operations\execute_operation.ts:79:30
at maybeCallback (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\utils.ts:450:19)
at executeOperation (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\operations\execute_operation.ts:79:23) {
[Symbol(errorLabels)]: Set(0) {}
}
MongoTopologyClosedError: Topology is closed
at processWaitQueue (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\sdam\topology.ts:904:42)
at Topology.selectServer (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\sdam\topology.ts:601:5)
at Topology.<anonymous> (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\sdam\topology.ts:252:17)
at node:internal/util:361:7
at new Promise (<anonymous>)
at Topology.selectServerAsync (node:internal/util:347:12)
at executeOperationAsync (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\operations\execute_operation.ts:110:20)
at D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\operations\execute_operation.ts:79:30
at maybeCallback (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\utils.ts:450:19)
at executeOperation (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\operations\execute_operation.ts:79:23) {
[Symbol(errorLabels)]: Set(0) {}
}
D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\sdam\topology.ts:904
drainWaitQueue(topology[kWaitQueue], new MongoTopologyClosedError());
^
MongoTopologyClosedError: Topology is closed
at processWaitQueue (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\sdam\topology.ts:904:42)
at Topology.<anonymous> (D:\Projects\chris-code-learning\chris-code-learning-server\node_modules\mongodb\src\sdam\topology.ts:252:17)
at node:internal/util:361:7
at new Promise (<anonymous>)
}
[nodemon] app crashed - waiting for file changes before starting...
Here is the way I connect to DB:
import {MongoClient} from 'mongodb'
import dotenv from 'dotenv'
dotenv.config()
const mongodbUrl = process.env.NODE_ENV === 'production' ? process.env.MONGODB_URL : process.env.MONGODB_URL_DEV
export const client = new MongoClient(mongodbUrl || '')
export const dbName = 'codeLearning'
async function main() {
await client.connect()
console.log(`Connected successfully to server at ${(new Date()).toString()}`)
}
main()
.catch(console.error)
Then I just import file into index.js
(project entry point):
// Run the app
server.listen(process.env.PORT, () => {
console.log(`App is running on port ${process.env.PORT}`)
})
Note: everything worked before upgrading nodejs.
What I’ve tried so far:
- change
localhost:27017
to0.0.0.0:27017
/127.0.0.1:27017
- turn off firewall
- change config:
# network interfaces
net:
port: 27017
ipv6: true
- going back to NodeJS 14.0.0
- upgrade mongodb driver from
^4.1.1
to^4.13.0
. If I get it right, according to compatibility matrix, MongoDB 6.0 & driver 4.13 are compatible, and should work stable in NodeJS 16.x.x environment.
Server seems to be able to connect to MongoDB, but it happens randomly (at least I cannot predict it). Compass works fine, MongoDB process also runs: