Home > Software engineering >  Next.js error cannot find module 'next/headers'
Next.js error cannot find module 'next/headers'

Time:11-07

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.

CodePudding user response:

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 is that in v4.12.x with next/headers package from next@12, maybe it is to support getting the session in React Server Components for example.

Edit package.json, maybe it fix your issue:

{
  "next-auth": "4.15.1"
}
  • Related