{"id":"dfc92e665a9bd3ea","repo":"prisma/prisma","slug":"mongodb-url-is-required-dfc92e","errorCode":null,"errorMessage":"MONGODB_URL is required","messagePattern":"MONGODB_URL is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"examples/bundle-size/src/mongo/main.ts","lineNumber":10,"sourceCode":"import mongo from '@prisma-next/mongo/runtime';\nimport { contract } from './contract';\n\nconst db = mongo({ contract });\n\nasync function main(): Promise<void> {\n  const url = process.env['MONGODB_URL'];\n  const dbName = process.env['MONGODB_DB'] ?? 'bundle_size';\n  if (!url) {\n    throw new Error('MONGODB_URL is required');\n  }\n  const runtime = await db.connect({ url, dbName });\n  try {\n    const plan = db.query.from('notes').limit(10).build();\n    const notes = await runtime.execute(plan).toArray();\n    console.log(JSON.stringify(notes, null, 2));\n  } finally {\n    await db.close();\n  }\n}\n\nawait main();\n","sourceCodeStart":1,"sourceCodeEnd":23,"githubUrl":"https://github.com/prisma/prisma/blob/1243cdffbec0506c595ac8fd5865fe258c336fa0/examples/bundle-size/src/mongo/main.ts#L1-L23","documentation":"Thrown by the bundle-size MongoDB example (the runtime variant that calls mongo({ contract }) directly) when MONGODB_URL is unset. Same purpose as the emit variant but on the hand-written runtime path: it needs the URL before db.connect can be invoked. Failing early avoids a misleading lower-level connection error.","triggerScenarios":"Running the non-emit mongo example (entrypoint importing @prisma-next/mongo/runtime and the local contract) with MONGODB_URL absent from process.env. Happens on direct execution of src/mongo/main.ts.","commonSituations":"Missing .env in the example workspace, running with a stripped environment in CI, or copying the snippet into another project without porting the env setup.","solutions":["Set MONGODB_URL in the shell or .env before launching the runtime example.","Confirm MONGODB_DB (optional, defaults to 'bundle_size') matches your target database.","Ensure the process is launched from the example directory where .env is loaded.","Add the variable to the CI matrix job that runs this example."],"exampleFix":"// before\nprocess.env // { MONGODB_DB: 'bundle_size' }\n// after\nprocess.env // { MONGODB_URL: 'mongodb://localhost:27017', MONGODB_DB: 'bundle_size' }","handlingStrategy":"validation","validationCode":"const url = process.env['MONGODB_URL'];\nconst dbName = process.env['MONGODB_DB'] ?? 'bundle_size';\nif (typeof url !== 'string' || url.length === 0) {\n  console.error('MONGODB_URL is required');\n  process.exit(1);\n}","typeGuard":"const isMongodbUrl = (v: unknown): v is string =>\n  typeof v === 'string' && /^mongodb(\\+srv)?:\\/\\/.+/.test(v);","tryCatchPattern":"try {\n  await main();\n} catch (error) {\n  if (error instanceof Error && error.message === 'MONGODB_URL is required') {\n    console.error('Set MONGODB_URL (and optionally MONGODB_DB) before running.');\n    process.exit(1);\n  }\n  throw error;\n}","preventionTips":["Share one connection-config module between main.ts and main-emit.ts so the var is read consistently.","Validate the URL shape (mongodb:// or mongodb+srv://) rather than just truthiness.","Load .env once at the example entry so both generated and hand-written mains see the var.","Document MONGODB_URL / MONGODB_DB in the example README."],"tags":["config","environment","mongodb","examples","bundle-size","runtime"],"analyzedSha":"1243cdffbec0506c595ac8fd5865fe258c336fa0","analyzedAt":"2026-08-01T19:42:02.087Z","schemaVersion":2}