Promise must be a function, got undefined

I currently have a SvelteKit/NodeJS project that needs to connect to MongoDB. I’ve made a file to handle caching an instance of a MongoDB client to use, but when I run the code, it returns the error:

Promise must be a function, got undefined


Here is the file that I am using to connect to my DB:

import { MongoClient } from 'mongodb';
import dotenv from 'dotenv';

export const MONGODB_URI = process.env['MONGODB_URI'];
export const MONGODB_DB = "ttrack";

    throw new Error('Please define the mongoURI property inside config/default.json');

if (!MONGODB_DB) {
    throw new Error('Please define the mongoDB property inside config/default.json');

 * Global is used here to maintain a cached connection across hot reloads
 * in development. This prevents connections growing exponentially
 * during API Route usage.
let cached = global.mongo;

if (!cached) {
    cached = global.mongo = { conn: null, promise: null };

export const connectToDatabase = async () => {
    if (cached.conn) {
        return cached.conn;

    if (!cached.promise) {
        const opts = {
            useNewUrlParser: true,
            useUnifiedTopology: true

        cached.promise = MongoClient.connect(MONGODB_URI, opts).then((client) => {
            return {
                db: client.db(MONGODB_DB)
    cached.conn = await cached.promise;
    return cached.conn;

I’m unsure how to proceed with this, as I haven’t been able to find any other documentation on this error for MongoDB (I have found some for mongoose, but they do not apply to my project). Could it possibly be a localhost error? I tracked it down to a function called require_promise_provider, but I have no clue where to go from there. Any help would be awesome!

Hi @Ali_Mosallaei,
Your code looks fine to me.
In my opinion, the problem is with the code not attached to your message.
If you create git repository and upload there the part of your code that is enough to reproduce your problem it may be easier to help you then.

Did you ever solve this issue? I’m running into it (along with quite a few other headaches with mongodb + sveltekit)