Next.js 错误找不到模块“next/headers”

Arj*_*jun 6 node.js reactjs next.js next-auth

当我在小组项目中使用我的队友开发的登录功能时遇到了这个问题,显然我是唯一面临这个问题的人。

npm 版本:8.19.3 节点版本:16.17.0

package.json :-

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "npm run prettify && next dev",
    "build": "npm run prettify && next build",
    "start": "next start",
    "lint": "npm run prettify && next lint",
    "prettify": "prettier --write \"**/*.{js, jsx}\""
  },
  "dependencies": {
    "@emotion/react": "11.10.4",
    "@emotion/server": "11.10.0",
    "@emotion/styled": "11.10.4",
    "@mui/icons-material": "5.10.6",
    "@mui/material": "5.10.6",
    "@mui/x-date-pickers": "^5.0.3",
    "cryptr": "^6.0.3",
    "dayjs": "^1.11.5",
    "intl": "^1.2.5",
    "mui-file-dropzone": "^4.0.2",
    "next": "12.2.5",
    "next-auth": "^4.12.0",
    "normalize.css": "^8.0.1",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-share": "^4.4.1",
    "react-toastify": "^9.0.8",
    "swr": "^1.3.0",
    "universal-cookie": "^4.0.4"
  },
  "devDependencies": {
    "eslint": "8.23.1",
    "eslint-config-next": "12.3.1",
    "eslint-config-prettier": "8.5.0",
    "prettier": "2.7.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

错误 :-

error - Error: Cannot find module 'next/headers'
Run Code Online (Sandbox Code Playgroud)

我已尝试删除 node_modules 和 .next 文件夹,但我仍然面临这个问题。

Gon*_*ani 6

也就是说,当您将Nextjs更新到版本 13 时next-auth,该版本适用于next@13用户,在您使用版本 12 的情况下,根本原因是v4.12.x来自next@12的next/headers包出现错误,因此可能是支持例如,在 React Server 组件中获取会话。

编辑 package.json,它应该可以解决您的问题:

{
  "next-auth": "4.15.1"
}
Run Code Online (Sandbox Code Playgroud)