If you by any chance face this error whiles building a Next.js application, remember that for things to work, mongoose must be used on the server side, not on the client. Remember to have "use server"
at the top of the file where you call mongoose.connect()
or any other mongoose function for that matter. That solved my problem.