Error library

Clerk: auth() was called but Clerk can't detect usage of clerkMiddleware()

Clerk throws this when auth() runs on a request that didn't pass through clerkMiddleware(). Make sure the middleware file is in the right location and its matcher covers the route that calls auth().

Get this exact error fixed — $49 Quick FixFixed within scope, or we keep working until it is · Read-only access Not sure it's this error? $149 diagnosis

Cause: Missing or misplaced middleware file, or a matcher that excludes the route.

How to fix it

  1. Create the Next.js middleware file exporting clerkMiddleware() at the project root (or in src/ if you use it).
  2. Use a matcher that includes your pages and /(api|trpc)(.*) routes.
  3. Make sure the file name and location match your Next.js version's convention.
  4. Restart the dev server after adding it.

Example

Illustrative example, not from a customer project.

+ import { clerkMiddleware } from '@clerk/nextjs/server'
+ export default clerkMiddleware()
+ export const config = { matcher: ['/((?!_next|.*\\..*).*)', '/(api|trpc)(.*)'] }

FAQ

I have a middleware file but still get it.

It may be in the wrong folder (e.g. not inside src/) or the matcher skips that route.

Do API routes need it?

Yes, if they call auth().

QUICK FIX · $49 · THIS ERROR ONLY

Get this exact error fixed — $49 Quick FixFixed within scope, or we keep working until it is · Read-only access Not sure it's this error? $149 diagnosis

One error, one root cause, fixed in your app. If it turns out bigger, we tell you before doing more, and your $49 is credited toward the fix, diagnosis or Launch Path if you book within 14 days. Fixing three? The 3-Error Pack is $119.

Bigger job: More than one auth or data issue? Auth & database security fix, from $400.

Built with Lovable, Bolt, Replit, Cursor or v0? See rescue by platform and the security checklist.

Check it yourself first, free

$49Fix this error