skip to Main Content

There are a lot of errors in my application console.
How to fix it?

Next js 13.2 / app directory

Import trace for requested module:
./node_modules/@aws-sdk/util-user-agent-node/dist-cjs/is-crt-available.js
./node_modules/@aws-sdk/util-user-agent-node/dist-cjs/index.js
./node_modules/@aws-sdk/client-cognito-identity/dist-cjs/runtimeConfig.js
./node_modules/@aws-sdk/client-cognito-identity/dist-cjs/CognitoIdentityClient.js
./node_modules/@aws-sdk/client-cognito-identity/dist-cjs/index.js
./node_modules/@aws-sdk/credential-providers/dist-cjs/fromCognitoIdentity.js
./node_modules/@aws-sdk/credential-providers/dist-cjs/index.js
./node_modules/mongoose/node_modules/mongodb/lib/deps.js
./node_modules/mongoose/node_modules/mongodb/lib/index.js
./node_modules/mongoose/lib/index.js
./node_modules/mongoose/index.js
./Total/db/connect.js
./Part/ssr/Ssr.js
./app/(pages)/profile/page.js

./node_modules/mongoose/node_modules/mongodb/lib/bson.js
Module not found: Can't resolve 'bson-ext' in 'C:Users79833WebstormProjectspartAppnode_modulesmongoosenode_modulesmongodblib'

Import trace for requested module:
./node_modules/mongoose/node_modules/mongodb/lib/bson.js
./node_modules/mongoose/node_modules/mongodb/lib/index.js
./node_modules/mongoose/lib/index.js
./node_modules/mongoose/index.js
./Total/db/connect.js
./Part/ssr/Ssr.js
./app/(pages)/profile/page.js

./node_modules/mongoose/node_modules/mongodb/lib/deps.js
Module not found: Can't resolve 'kerberos' in 'C:Users79833WebstormProjectspartAppnode_modulesmongoosenode_modulesmongodblib'

Import trace for requested module:
./node_modules/mongoose/node_modules/mongodb/lib/deps.js
./node_modules/mongoose/node_modules/mongodb/lib/index.js
./node_modules/mongoose/lib/index.js
./node_modules/mongoose/index.js
./Total/db/connect.js
./Part/ssr/Ssr.js
./app/(pages)/profile/page.js

./node_modules/mongoose/node_modules/mongodb/lib/deps.js
Module not found: Can't resolve '@mongodb-js/zstd' in 'C:Users79833WebstormProjectspartAppnode_modulesmongoosenode_modulesmongodblib'

Import trace for requested module:
./node_modules/mongoose/node_modules/mongodb/lib/deps.js
./node_modules/mongoose/node_modules/mongodb/lib/index.js
./node_modules/mongoose/lib/index.js
./node_modules/mongoose/index.js
./Total/db/connect.js
./Part/ssr/Ssr.js
./app/(pages)/profile/page.js

I have tried updating and reinstalling packages. The error does not go away.

These are not all errors. There are many others. They also look

2

Answers


  1. In nextjs you have to export the schema like this this will fix your issue

    import mongoose from "mongoose";
    const Schema = mongoose.Schema;
    
    const ProductSchema = new mongoose.Schema({
        name:{type:String, required:true, unique:true},
        image:{type:String, required:true},
    },{timestamps : true})
    
    
    export default mongoose.models.Product || mongoose.model("Product", ProductSchema); //like this try to export every model in you app like this and it should work
    
    Login or Signup to reply.
  2. These are the things i’ve learned by spending time using mongoose 7 with latest nextjs version:

    Make sure that mongoose is not imported inside client components ! (Which were creating Can’t resolve errors that you are facing)

    And if you are using mongoose 7, I have to put serverComponentsExternalPackages option in next.config.js (even with the fix given here https://github.com/vercel/next.js/issues/42277)

    /** @type {import("next").NextConfig} */
    module.exports = {
        experimental: { appDir: true, serverComponentsExternalPackages: ["mongoose"] },
        webpack(config) {
            config.experiments = { ...config.experiments, topLevelAwait: true };
            return config;
        }
    };
    

    I would also change your export by

    const Product = mongoose.models && "Product" in mongoose.models ? mongoose.models. Product : mongoose.model("Product", PostSchema);
    export default Product;
    

    my project https://github.com/mathieuguyot/adventures have mongoose 7 integrated with next 13.2.4, maybe it can help you in any way 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search