After some investigation, I’ve found the solution to my problem.
Because the error is from Node.js internal, not MongoDB driver, so the main issue is that Node.js cannot resolve the dns record. For my case, the DNS server which Node.js is connecting to is 127.0.0.53
, which I’ve had problem with before. You can check by using this code snippet:
import dns from "node:dns/promises";
console.log(await dns.getServers());
// [ '127.0.0.53' ]
To solve above issue, I use this snippet, which is explicit set the DNS server inside Node.j:
import dns from "node:dns/promises";
dns.setServers(["1.1.1.1"]);
I hope this solution can solve yours as well.