I am currently utilizing version ^13.5.3 of Next. I have created a middleware.js file in the pages/middleware.js directory
import { NextResponse } from "next/server";
export function middleware(req) {
...
}
This is resulting in the following error: ESLint: next/server should not be imported outside of pages/_middleware.js. Refer to: https://nextjs.org/docs/messages/no-server-import-in-page(@next/next/no-server-import-in-page)
The documentation states: Only import and use next/server in a file located within the project root directory: middleware.{js,ts}.
I tried moving it to the root directory.
https://i.sstatic.net/fPyLz.png
However, I received this new error: ESLint: next/server should not be imported outside of pages/_middleware.js. See: https://nextjs.org/docs/messages/no-server-import-in-page(@next/next/no-server-import-in-page)
Upon changing it to pages/_middleware.js, I encountered an error while building. This has left me feeling quite perplexed.
https://i.sstatic.net/5ULWi.png
My Inquiries:
- Should the middleware file be prefixed with "_"?
- Where should I locate the middleware file in order to utilize "NextResponse"?