{"record":{"id":"62a5270e65af49e1","repo":"nextauthjs/next-auth","slug":"next-auth-middleware-is-deprecated-if-you-are-n","errorCode":null,"errorMessage":"\"next-auth/middleware\" is deprecated. If you are not ready to migrate, keep using \"next-auth@4\".\nRead more on https://authjs.dev/getting-started/migrating-to-v5","messagePattern":"\"next-auth/middleware\" is deprecated\\. If you are not ready to migrate, keep using \"next-auth@4\"\\.\nRead more on https://authjs\\.dev/getting-started/migrating-to-v5","errorType":"exception","errorClass":"ReferenceError","httpStatus":null,"severity":"critical","filePath":"packages/next-auth/src/middleware.ts","lineNumber":9,"sourceCode":"/**\n * :::warning Deprecated\n * This module is replaced in v5. Read more at: https://authjs.dev/getting-started/migrating-to-v5#authenticating-server-side\n * :::\n *\n * @module middleware\n */\n\nthrow new ReferenceError(\n  [\n    '\"next-auth/middleware\" is deprecated. If you are not ready to migrate, keep using \"next-auth@4\".',\n    \"Read more on https://authjs.dev/getting-started/migrating-to-v5\",\n  ].join(\"\\n\")\n)\n\nexport {}\n","sourceCodeStart":1,"sourceCodeEnd":17,"githubUrl":"https://github.com/nextauthjs/next-auth/blob/a1a16a5a7780488c7449feece410033f445d0b31/packages/next-auth/src/middleware.ts#L1-L17","documentation":"The next-auth/middleware module is a hard-deprecated re-export that immediately throws a ReferenceError at import time in next-auth v5 (Auth.js). The module was removed in the v5 migration; the throw exists purely to give a actionable message pointing to the migration guide, telling users to keep using next-auth@4 if they aren't ready to migrate.","triggerScenarios":"Importing \"next-auth/middleware\" (e.g. `import { withAuth } from \"next-auth/middleware\"` in middleware.ts) after upgrading to next-auth v5. The ReferenceError fires the moment the module is loaded, before any middleware code runs.","commonSituations":"Upgrading from next-auth@4 to v5 (Auth.js) without updating middleware.ts; following v4 tutorials while installing the latest package; copy-pasted middleware wrappers like withAuth/defaultMiddleware that no longer exist.","solutions":["Remove the next-auth/middleware import and follow the v5 migration guide at authjs.dev/getting-started/migrating-to-v5.","Replace middleware-based auth with server-side checks: call auth() from next-auth inside your pages/route handlers (server components).","If you need edge middleware protection, export an auth config and wrap logic manually using the exported auth function suitable for edge runtime.","If not ready to migrate, pin next-auth@4 in package.json until the codebase is updated."],"exampleFix":"// before\nimport { withAuth } from \"next-auth/middleware\"\nexport default withAuth({ pages: { signIn: \"/login\" } })\n// after\nimport NextAuth from \"next-auth\"\nexport const { auth } = NextAuth({ ... })\n// middleware.ts\nexport default auth((req) => {\n  if (!req.auth) return Response.redirect(new URL(\"/login\", req.url))\n})","handlingStrategy":"fallback","validationCode":"// Avoid at build time:\n// import \"next-auth/middleware\" // <- throws ReferenceError in v5; do not import.\n","typeGuard":null,"tryCatchPattern":"// Cannot be caught — throws at module load. Fix the import instead:\n// Remove: import { withAuth } from \"next-auth/middleware\"\n// Use: export default auth((req) => { /* check req.auth */ }) in middleware.ts","preventionTips":["After upgrading to next-auth v5, grep the codebase for 'next-auth/middleware' and remove it.","Read the v5 migration guide before upgrading: authjs.dev/getting-started/migrating-to-v5.","Replace middleware-based protection with server-side auth() checks or an edge-compatible auth wrapper.","Pin exact versions in package.json so major upgrades are intentional."],"tags":["next-auth","deprecation","migration","import-error"],"backgroundTag":"deprecated-module-removed","analyzedSha":"a1a16a5a7780488c7449feece410033f445d0b31","analyzedAt":"2026-08-28T21:52:38.200Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}