Clerk: auth() was called but it looks like you aren't using authMiddleware in your middleware file

I'm using the nextjs app dir, and my middleware is definitely being called:
export default authMiddleware({
afterAuth: async (auth, request, evt) => {
console.log("hello from middleware")
}
});


export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};
export default authMiddleware({
afterAuth: async (auth, request, evt) => {
console.log("hello from middleware")
}
});


export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};
But I still get the error that I'm not using authMiddleware when I use
import { UserButton, auth } from '@clerk/nextjs';

const user = auth()
import { UserButton, auth } from '@clerk/nextjs';

const user = auth()
Any suggestions?
11 Replies
fasm
fasm2y ago
this is my file structure
El Remolino
El Remolino2y ago
Im actually having the same issue as OP. And my file structure follows fasm's. I tried wrapping the console log in an annonymous function, no luck.
Michael Schaufelberger
Strange, I have a similar setup. Altough, I'm using getAuth() instead of auth() on the server: import { getAuth } from '@clerk/nextjs/server'; And const auth = useAuth(); on the client. Where do you actually call auth()?
Diogo
Diogo2y ago
Try moving the middleware file outside the src folder
fasm
fasm2y ago
I spent ages on this, and ended up fixing it by updating my version of node to v20
El Remolino
El Remolino2y ago
apologies for the noob question, but how do you go about doing that? Do I need to update the node version globally or just from within my repo?
fasm
fasm2y ago
freeCodeCamp.org
How to Update Node and NPM to the Latest Version
Node is a runtime environment that allows developers to execute JavaScript code outside the browser, on the server-side. NPM, on the other hand, is a package manager for publishing JavaScript packages (also known as Node modules) to the npm registry [https://www.npmjs.com/]. You can also use it to install packages to
fasm
fasm2y ago
and you upgrade it globally
mansavesbabyweldedtocar
Had this issue just now. What worked was: - Moving the king middleware.ts to the root of the src folder - alongside app. - Upgrading to Node.js 20 - I did this with nvm since I'm lucky enough to be on Linux - Removing publicRoutes was necessary for the first time running but I'm not certain this had any effect
Steven-sensei
Steven-sensei17mo ago
Btw nvm is available on windows fnm to :p
mansavesbabyweldedtocar
Aah yeah but i had to look for a package called nvm-windows once. Keep in mind its a separate package. Come as an MSI i believe though
Want results from more Discord servers?
Add your server