Search code examples
javascriptnode.jsmongoosenext.js

Mongoose Cannot read properties of undefined (reading 'prototype')


I'm running Mongoose 7.0.1 on Next JS 13.2.2 and React 18.2.0.

I've been looking high and low to find a solution to this issue. I just want to connect to a MongoDB, however, when I import mongoose to the project to connect. This error occurs. Am I doing something wrong? Error Log

The Code in question

So far I tried to fiddle with the importing process [ie. change from ES6 to Casual JS with "require"] I don't have a clue on what to do seeing as its in the Node Module itsself. I tried different versions of Mongoose as well. But to no avail.

EDIT >> Here's some screen shots of how I implemented mongoose library/mongo/index.ts app/api/route.ts


Solution

  • In next.config.js file write the below code:

    experimental: {
      appDir: true,
      serverComponentsExternalPackages: ["mongoose"],
    },