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

**Code:** `The uri parameter to openUri() must be a string, got "undefined"` · **Category:** MongoDB · **Seen on:** MongoDB, Node.js

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

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/mongodb/mongoose-uri-must-be-a-string-undefined)
