Next.js Discord

Discord Forum

Clarify docs where to put middleware.ts

Answered
Greenish Elaenia posted this in #help-forum
Open in Discord
Avatar
Greenish ElaeniaOP
For App Router, at https://nextjs.org/docs/app/building-your-application/authentication#protecting-routes-with-middleware, it says:

Setting Up Middleware:
Create a middleware.ts or .js file in your project's root directory.
Include logic to authorize user access, such as checking for authentication tokens.

Given the project structure looks like this (image), what does "project root directory" mean?
I tried putting a middleware.ts in /middleware folder but it doesn't seem to be running
Image
Answered by averydelusionalperson
Image
View full answer

23 Replies

Avatar
it should be at root level
no need for middleware folder
just ./middleware.ts
Avatar
Image
Answer
Avatar
Yellow Crazy Ant
Is there any effect if you use a folder or not
Avatar
you can't use a folder 🤷‍♂️
it needs to be in root path
Avatar
Yellow Crazy Ant
Okay but my question, is what are the effects or disadvantage of putting it in a folder
Avatar
you can't put it in a folder man
the file needs to be in the root path
Avatar
Yellow Crazy Ant
Okay
Avatar
so, your problem is solved?
Avatar
Yellow Crazy Ant
Not really sir, because all I know is not to put it in a folder, so I don't know what damage that might come up
Avatar
why do you want to put it in a folder?
Avatar
Yellow Crazy Ant
I don't know just feel is nice to do
Avatar
lmao, but you can't
Avatar
Next.js only reads it in the root dir or the /src dir
if you put it in a folder then Next.js can't read the file. its not a matter of common practice, but its part of the Next.js file convention
Avatar
Yellow Crazy Ant
Thank you for explanation
Really help me alot
Avatar
no worriess
Avatar
Greenish ElaeniaOP
Tried it in a middleware folder, doing it that way means middleware.ts won't run
Avatar
Yellow Crazy Ant
Okay