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().
Cause: Missing or misplaced middleware file, or a matcher that excludes the route.
How to fix it
- Create the Next.js middleware file exporting
clerkMiddleware()at the project root (or insrc/if you use it). - Use a matcher that includes your pages and
/(api|trpc)(.*)routes. - Make sure the file name and location match your Next.js version's convention.
- 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().
Get a $149 diagnosis See pricing Still exposed or still logged out? Get an auth & database security fix (Supabase, Firebase).
- Fixed, or we keep working free until it is (within the written scope).
- A human reviews every fix before it ships.
- read-only access; fixes arrive as pull requests you merge
- Your $149 diagnosis is credited in full toward any paid job.