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.
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 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)
.envmissing in the deploy- Env var not set on the hosting platform
- Next.js client-side code reading a server-only variable
⚡ Quick fix
- Call
require("dotenv").config()at the very top - Match the variable name exactly
- Set the env var in the hosting dashboard and redeploy
Detailed fix by platform
Node.js
console.log("URI set:", Boolean(process.env.MONGODB_URI));
How to diagnose
- Variable — Defined at runtime?
- dotenv — Loaded first?
- 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:
Similar errors
- buffering timed out MongooseError: Operation `users.find()` buffering timed out after 10000ms
- CastError: Cast to Date/Number/Boolean failed Mongoose: CastError: Cast to date failed for value "abc" (type string) at path "createdAt"
- MissingSchemaError: Schema hasn't been registered for model Mongoose: MissingSchemaError: Schema hasn't been registered for model "User". Use mongoose.model(name, schema)
Most viewed in MongoDB
- Document failed validation MongoDB: MongoServerError: Document failed validation (code 121)
- bad auth : Authentication failed MongoDB: MongoServerError: bad auth : Authentication failed (code 8000 / 18)
- VersionError: No matching document found for id Mongoose: VersionError: No matching document found for id "5f..." version 3 modifiedPaths "items"
Other ways to find it
Where this error sits
Origin: Data. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 5 Nov 2026