导入所请求的模块 / next js 13 / 应用程序目录 / Mongoose MongoDB

huangapple go评论69阅读模式
英文:

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

问题

我的应用程序控制台中有很多错误。
如何修复它?

Next.js 13.2 / 应用目录

请求模块的导入跟踪:
./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
未找到模块:无法解析 'bson-ext' 在 'C:\Users833\WebstormProjects\partApp\node_modules\mongoose\node_modules\mongodb\lib' 中

请求模块的导入跟踪:
./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
未找到模块:无法解析 'kerberos' 在 'C:\Users833\WebstormProjects\partApp\node_modules\mongoose\node_modules\mongodb\lib' 中

请求模块的导入跟踪:
./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

我已经尝试更新和重新安装包。错误没有消失。

这些并不是所有的错误。还有很多其他错误。

英文:

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:\Users833\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:\Users833\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:\Users833\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

答案1

得分: 2

以下是您要翻译的内容:

These are the things i've learned by spending time using mongoose 7 with latest nextjs version:

确保不要在客户端组件内导入mongoose!(这会导致您面临的"Can't resolve"错误)

而且,如果您正在使用mongoose 7,我必须在next.config.js中添加serverComponentsExternalPackages选项(即使已经使用此处提供的修复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;
    }
};

我还会更改您的导出方式:

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

我的项目https://github.com/mathieuguyot/adventures已经将mongoose 7与next 13.2.4集成在一起,也许对您有所帮助 导入所请求的模块 / next js 13 / 应用程序目录 / Mongoose MongoDB

英文:

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 导入所请求的模块 / next js 13 / 应用程序目录 / Mongoose MongoDB

答案2

得分: 0

在Next.js中,您需要像这样导出模式,这将解决您的问题:

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); //像这样尝试导出应用程序中的每个模型,应该可以正常工作
英文:

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

答案3

得分: 0

你有两种解决方法来解决这个问题。

安装 encoding
正如在其他问题中建议的,你可以在本地安装 encoding。

npm i -D encoding

在 next.config.js 中忽略警告
如果你希望避免安装另一个(未使用的)库,你可以简单地通过 next.config.js 中的 webpack 配置忽略警告。

webpack: config => {
  /* 在 `node-fetch` v2 上,`supabase-js` 使用,
  为了 `.textConverted` 可选地需要 `encoding` 包,
  这意味着它不在 `node-fetch` 依赖中。
  参见:https://github.com/node-fetch/node-fetch/issues/412。
  由于 `encoding` 默认情况下不是依赖的一部分,当与 webpack 一起使用时,
  它将引发一个警告消息。
  这可以被忽略,因为它不会影响任何正常运行。 */
  config.ignoreWarnings = [
    { module: /node_modules\/node-fetch\/lib\/index\.js/ },
    { file: /node_modules\/node-fetch\/lib\/index\.js/ },
  ];

  return config;
}

参考链接:NextJS Discussion Board

英文:

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

答案4

得分: -1

似乎有一些用户在处理一个错误时遇到了问题,我找到了一个可能有帮助的解决方案:

  1. 首先,检查您当前的Node.js版本,并将其更新到最新版本(Node.js 16)。
  2. 更新完成后,重新安装您的nodemodules。
  3. 进行构建。

这应该解决该问题。重要的是要确保严格按照这些说明进行操作,以避免进一步的问题。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月27日 12:51:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定