Import trace for requested module / next js 13 / app directory / mongoose mongodb
Asked Answered
S

6

6

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:\Users\79833\WebstormProjects\partApp\node_modules\mongoose\node_modules\mongodb\lib'

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:\Users\79833\WebstormProjects\partApp\node_modules\mongoose\node_modules\mongodb\lib'

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:\Users\79833\WebstormProjects\partApp\node_modules\mongoose\node_modules\mongodb\lib'

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

Scissure answered 27/2, 2023 at 4:51 Comment(0)
P
3

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 :)

Perplexity answered 28/3, 2023 at 17:6 Comment(0)
J
0

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
Jeweljeweler answered 4/3, 2023 at 7:59 Comment(0)
B
0

You have 2 solutions to resolve this.

Install encoding As suggested in other issues, you can install encoding on our own locally.

npm i -D encoding

Ignore warning in next.config.js If you prefer to avoid installing another (unused) lib, you can simply ignore the warning through webpack config inside next.config.js

webpack: config => {
  /* On `node-fetch` v2, that `supabase-js` uses,
  `encoding` package was optionally required for `.textConverted`
  which means it wasn't in `node-fetch` deps.
  See: https://github.com/node-fetch/node-fetch/issues/412.
  Since `encoding` is not part of the deps by default, when using with webpack,
  it will raise a warning message.
  This can be ignored as it doesn't prevent anything to work well. */
  config.ignoreWarnings = [
    { module: /node_modules\/node-fetch\/lib\/index\.js/ },
    { file: /node_modules\/node-fetch\/lib\/index\.js/ },
  ];

  return config;
}

Ref: NextJS Discussion Board

Burgoo answered 30/8, 2023 at 18:57 Comment(0)
H
0

To my best understanding i feel the "Import trace for requested module" message itself is not an error but rather a helpful tool/message for understanding the import chain in your Netjs project

Homoeroticism answered 28/2 at 10:50 Comment(0)
I
0

Was having this on Windows 10 and Next.js 14.2.15 and getting console errors that said There are multiple modules with names that only differ in casing as well as the import trace issue. VS code defaults to Powershell so I switched from using VSCode integrated terminal to cmd in another window and that fixed it.

Irremissible answered 12/10 at 22:41 Comment(0)
A
-1

It seems that some users are experiencing issues with a bug, and I have found a solution that may help:

  1. Firstly, check your current version of node js, and update it to the latest version (node 16).
  2. Once updated, reinstall your nodemodules.
  3. Make a build.

This should resolve the issue. It is important to ensure that the instructions are followed carefully in order to avoid any further issues.

Althorn answered 8/5, 2023 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.