Next.js error cannot find module 'next/headers'
Asked Answered
I

1

6

Faced this issue when I was using login functionality developed by my teammate in a group project and apparently I am the only one facing this issue.

npm version: 8.19.3 node version: 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"
  }
}

Error :-

error - Error: Cannot find module 'next/headers'

I have tried deleting the node_modules and .next folders but I am still facing this issue.

Iraq answered 6/11, 2022 at 20:49 Comment(3)
I think it is a version mismatchPrecipitin
Yes changing next-auth to 4.15.0, fixes this issue.Iraq
Yes, that's the issue, anyway they should pass the experimental process.Lapidary
L
6

That is when you update Nextjs to version 13 with next-auth, which is intended for next@13 users, in your case using version 12 yet, the root cause the error in v4.12.x with next/headers package from next@12, so it could be to support getting the session in React Server Components for example.

Edit package.json, it should fix your issue:

{
  "next-auth": "4.15.1"
}
Lapidary answered 6/11, 2022 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.