The uri parameter to openUri() must be a string, got "undefined" MongoDB Origin: Data OSI layer 7 · Application Last updated 5 Nov 2026

MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` is a string

mongoose.connect() received undefined instead of a connection string — the environment variable wasn't loaded.

Exact message you may see MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined"The uri parameter to openUri() must be a string, got undefinedMake sure the first parameter to mongoose.connect() or mongoose.createConnection() is a stringMongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://" undefined
Seen on: MongoDB Node.js

Meaning

process.env.MONGODB_URI is undefined: .env not loaded (dotenv not required before use, wrong file path), the variable name differs, or the platform (Vercel/Heroku) lacks the setting. Load dotenv first (require('dotenv').config()), check the exact variable name, and log Boolean(process.env.MONGODB_URI).

Common causes

  • dotenv not loaded before connect()
  • Variable name mismatch (MONGO_URI vs MONGODB_URI)
  • .env missing in the deploy
  • Env var not set on the hosting platform
  • Next.js client-side code reading a server-only variable

⚡ Quick fix

  1. Call require("dotenv").config() at the very top
  2. Match the variable name exactly
  3. Set the env var in the hosting dashboard and redeploy

Detailed fix by platform

Node.js

  1. console.log("URI set:", Boolean(process.env.MONGODB_URI));

How to diagnose

  1. Variable — Defined at runtime?
  2. dotenv — Loaded first?
  3. Platform — Env set?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

buffering timed out — MongooseError: Operation `users.find()` buffering timed out after 10000ms (Node.js)buffering timed outCastError: Cast to Date/Number/Boolean failed — Mongoose: CastError: Cast to date failed for value "abc" (type string) at path "createdAt" (MongoDB)CastError: Cast to Da…MissingSchemaError: Schema hasn't been registered for model — Mongoose: MissingSchemaError: Schema hasn't been registered for model "User". Use mongoose.model(name, schema) (MongoDB)MissingSchemaError: S…MongoParseError — MongoDB: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://" (MongoDB)MongoParseErrorMongoParseError: options usenewurlparser / useunifiedtopology not supported — MongoParseError: option usenewurlparser is not supported (MongoDB)MongoParseError: opti…MongoParseError: Password contains unescaped characters — MongoParseError: URI malformed, cannot be parsed / Password contains unescaped characters (MongoDB)MongoParseError: Pass…OverwriteModelError: Cannot overwrite model once compiled — Mongoose: OverwriteModelError: Cannot overwrite `User` model once compiled. (MongoDB)OverwriteModelError: …process.env.X is undefined in the browser (NEXT_PUBLIC_) — Next.js: process.env.API_URL is undefined in Client Component / env variable not available in the browser (Next.js)process.env.X is unde…The uri p…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.