Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@next/next/no-server-import-in-page throws error on nested _middleware #32156

Closed
francofantini opened this issue Dec 6, 2021 · 6 comments
Closed
Labels
bug Issue was opened via the bug report template.

Comments

@francofantini
Copy link

What version of Next.js are you using?

12.0.7

What version of Node.js are you using?

v12.22.6

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

@next/next/no-server-import-in-page rule throws error on nested _middleware.js.

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

Expected Behavior

This rules should behave the same way for nested and root _middleware.js

To Reproduce

Create 2 _middleware.js, one in ./pages/_middleware.js and the other in ./pages/nested/_middleware.js with the same code and import import { NextResponse } from "next/server";.

Code Example: https://replit.com/@francofantini/no-server-import-in-page-bug

Run npm run lint on this repo and you'll see that the rule @next/next/no-server-import-in-page fails for ./pages/link/_middleware.js but not for ./pages/_middleware.js.

$ npm run lint

> next12@0.1.0 lint /home/runner/no-server-import-in-page-bug
> next lint


./pages/link/_middleware.js
1:1  Error: 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

info  - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! next12@0.1.0 lint: `next lint`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the next12@0.1.0 lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2021-12-06T00_35_02_565Z-debug.log
@francofantini francofantini added the bug Issue was opened via the bug report template. label Dec 6, 2021
@matamatanot
Copy link
Contributor

matamatanot commented Dec 6, 2021

I have the same.

./src/pages/live/[id]/_middleware.tsx
1:1 Error: 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

@francofantini
Copy link
Author

@matamatanot which version of eslint are you using?

I'm on eslint@8.4.0

@francofantini
Copy link
Author

I'm able to reproduce this error with eslint@7.32.0 so I don't think this is related to eslint's version.

I've narrow down the next version where this error first appear to next@10.0.5: https://github.com/vercel/next.js/releases/tag/v12.0.5
This version introduced a patch for next-server.

@matamatanot
Copy link
Contributor

This seems to have already been resolved in canary(#32139).

@thibautsabot
Copy link
Contributor

Hello!

Yes it has been fixed and released on v12.0.8-canary.0.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

4 participants