nextjs 导入但不调用函数抛出未找到模块:错误:无法解析“dns" [英] nextjs import but don't invoke function throws Module not found: Error: Can't resolve 'dns'

查看:56
本文介绍了nextjs 导入但不调用函数抛出未找到模块:错误:无法解析“dns"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目(NextJS)工作正常,突然我遇到了问题ModuleNotFoundError.特别是在 nextJs 的动态路由的情况下.

My project(NextJS) was working fine and suddenly I am experiencing the issue ModuleNotFoundError. Particularly in the case of dynamic routing of nextJs.

我看到的错误是:Module not found: Error: Can't resolve 'dns'

pages目录下pages/programs/[programtype]/[program].jsx导入mongo的时候会抛出:

In the pages directory pages/programs/[programtype]/[program].jsx when mongo is imported, it throws:

ModuleNotFoundError: Module not found: Error: Can't resolve 'dns' in 'node_modules/mongodb/lib'

完整的错误转储:

ModuleNotFoundError: Module not found: Error: Can't resolve 'dns' in '/project-path/node_modules/mongodb/lib'
    at /project-path/node_modules/webpack/lib/Compilation.js:925:10
    at /project-path/node_modules/webpack/lib/NormalModuleFactory.js:401:22
    at /project-path/node_modules/webpack/lib/NormalModuleFactory.js:130:21
    at /project-path/node_modules/webpack/lib/NormalModuleFactory.js:224:22
    at /project-path/node_modules/neo-async/async.js:2830:7
    at /project-path/node_modules/neo-async/async.js:6877:13
    at /project-path/node_modules/webpack/lib/NormalModuleFactory.js:214:25
    at /project-path/node_modules/enhanced-resolve/lib/Resolver.js:213:14
    at /project-path/node_modules/enhanced-resolve/lib/Resolver.js:285:5
    at eval (eval at create (/project-path/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:13:1)
    at /project-path/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:44:7
    at /project-path/node_modules/enhanced-resolve/lib/Resolver.js:285:5
    at eval (eval at create (/project-path/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:13:1)
    at /project-path/node_modules/enhanced-resolve/lib/Resolver.js:285:5
    at eval (eval at create (/project-path/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:25:1)
    at /project-path/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:67:43

推荐答案

这是nextjs中服务器端代码的一个真正微妙的问题.

This is a real subtle problem with server side code in nextjs.

错误是明显:您正在尝试在客户端代码中执行服务器端代码(mongo 查询).原因不明显,因为我确定您没有错误的代码...

The error is obvious: you're trying to execute server side code (mongo query) in a client side code. What isn't obvious is the cause, because I'm sure you don't have a wrong code...

经过一些调试后,我发现如果您导入 mongo 代码并且不使用它,则会引发此错误.请参阅下文了解如何避免它.

After some debug I discovered that this error is thrown if you import your mongo code and don't use it. See below to understand how to avoid it.

所以,这很好用:

import { findUsers } from '../lib/queries'

function Home({ users }) {
  return (
    <h1>Users list</h1>
    //users.map and so on...
  )
}

export async function getServerSideProps() {
  const users = await findUsers()
  return {
    props: {
      users: users
    }
  }
}

export default Home

虽然这会抛出错误:

import { findUsers } from '../lib/queries'

function Home({ users }) {
  return (
    <h1>Users list</h1>
    //users.map and so on...
  )
}

export async function getServerSideProps() {
  // call disabled to show the error
  // const users = await findUsers()
  return {
    props: {
      users: [] //returned an empty array to avoid other errors
    }
  }
}

export default Home

如何避免

为避免此错误,只需删除组件中的任何服务器端代码导入如果您不使用它在 getServerSideProps 中.

这篇关于nextjs 导入但不调用函数抛出未找到模块:错误:无法解析“dns"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆