Next.js Discord

Discord Forum

My console.log calls don't appear on terminal.

Answered
Satin Angora posted this in #help-forum
Open in Discord
Avatar
Satin AngoraOP
I'm trying to debug middleware function, but my console.log calls don't appear on terminal, or on Chrome console.

Why it may be?
Answered by Dayo
it should be in your src folder
View full answer

15 Replies

Avatar
How does the middleware file look like? You could be returning prematurely from the function, or your log call could be unreachable.
Avatar
Satin AngoraOP
I make the call in the top of middleware function. I have two functions and a config object in the file
function getLocale(req: NextRequest): string | undefined {
  ...
  return locale
}

export function middleware(req: NextRequest) {
  const pathname = req.nextUrl.pathname
  console.log("pathname: " + pathname)
  ...
}

export const config = {
  matcher: ['/((?!api|_next/static|_next/image|res|favicon.ico).*)']
}
Maybe I don't understand how middleware works.
Avatar
Doberman Pinscher
is it in your root dir
Avatar
It has to be placed at the root of your project in order to be executed
Avatar
Satin AngoraOP
I know. It is in my root folder
Avatar
Have you tried killing the server and restarting it?
Avatar
Satin AngoraOP
Yes. I also tried to navigate to the routes using lang parameter. (like localhost:3000/en)
Avatar
Clearly the middleware call is skipped for some reason. Are you sure you have the file in ./ and not ./src?
Avatar
Satin AngoraOP
Yep:
Image
Avatar
it should be in your src folder
Answer
Avatar
Satin AngoraOP
@Dayo Why though? In documentation it says
Use the file middleware.ts (or .js) in the root of your project to define Middleware. For example, at the same level as pages or app, or inside src if applicable.
Oh wait, while it says root, it also says at the same level as app...
Avatar
Satin AngoraOP
@Dayo @not-milo.tsx Moving middleware into /src/ solved the issue! TY all
Avatar
Great :success: