I am trying to deploy a Next.js app that uses MongoDB on Vercel. The development environment works fine, but Vercel returns the following error:
Error: querySrv ETIMEOUT _mongodb._tcp.cluster0.nxug582.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (node:internal/dns/promises:291:17) {
errno: undefined,
code: 'ETIMEOUT',
syscall: 'querySrv',
hostname: '_mongodb._tcp.cluster0.nxug582.mongodb.net'
I have successfully integrated MongoDB with Vercel, but I’m unable to resolve this issue. If anyone has any ideas, please let me know.
Here is the MongoDB process:
import { MongoClient } from "mongodb"
if (!process.env.MONGODB_URI) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"')
}
const uri = process.env.MONGODB_URI
const options = {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
};
let client
let clientPromise
if (process.env.NODE_ENV === "development") {
let globalWithMongo = global
if (!globalWithMongo._mongoClientPromise) {
client = new MongoClient(uri, options);
globalWithMongo._mongoClientPromise = client.connect();
}
clientPromise = globalWithMongo._mongoClientPromise;
} else {
client = new MongoClient(uri, options);
clientPromise = client.connect();
}
export default clientPromise;
I have followed the MongoDB integration instructions for Vercel, ensured that my environment variables for MongoDB are correctly set in Vercel and tested my MongoDB connection locally, and it works without any issues.
I expected the app to deploy successfully on Vercel without any errors, as it does in my local development environment.