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
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

How does the middleware file look like? You could be returning prematurely from the function, or your log call could be unreachable.
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.
Doberman Pinscher
is it in your root dir
It has to be placed at the root of your project in order to be executed
Satin AngoraOP
I know. It is in my root folder
Have you tried killing the server and restarting it?
Satin AngoraOP
Yes. I also tried to navigate to the routes using lang parameter. (like localhost:3000/en)
Clearly the middleware call is skipped for some reason. Are you sure you have the file in ./ and not ./src?
Satin AngoraOP
Yep:
@Satin Angora Yep:
it should be in your src folder
Answer
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...
Satin AngoraOP
@Dayo @not-milo.tsx Moving middleware into /src/ solved the issue! TY all
Great :success: