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

MongoError@http://localhost:3000/node_modules/.vite/mongodb.js?v=5d182f1b:101:11
MongoDriverError@http://localhost:3000/node_modules/.vite/mongodb.js?v=5d182f1b:145:9

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

import { MongoClient } from 'mongodb';
import dotenv from 'dotenv';
dotenv.config();

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

if (!MONGODB_URI) {
    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 {
                client,
                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.
Thanks,
Rafael,

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

Any news here?
import {MongoClient} from “mongodb”;
leads to below error. im about to cry

MongoInvalidArgumentError: Promise must be a function, got undefined
    MongoError error.ts:129
    MongoDriverError error.ts:200
    MongoAPIError error.ts:220
    MongoInvalidArgumentError error.ts:601
    validate promise_provider.ts:22
    set promise_provider.ts:28
    mongodb mongodb.js:6310
    __require2 chunk-OROXOI2D.js:16
    mongodb mongodb.js:6786
    __require2 chunk-OROXOI2D.js:16
    mongodb mongodb.js:7946
    __require2 chunk-OROXOI2D.js:16
    mongodb mongodb.js:8304
    __require2 chunk-OROXOI2D.js:16
    mongodb mongodb.js:29300
    __require2 chunk-OROXOI2D.js:16
    <anonymous> mongodb:1
client-manifest.js:26:44