question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thibautsabotcommented, Dec 6, 2021

Hello!

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

0reactions
balazsorban44commented, Jan 27, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NextJS Vercel deployment error Nested Middleware is not ...
All of a sudden my middleware stopped working in deployment. The error is: > Build error occurred NestedMiddlewareError: Nested Middleware ...
Read more >
nested-middleware - Next.js
Nested Middleware. Why This Error Occurred. You are defining a Middleware file in a location different from <root>/middleware , which is not allowed....
Read more >
Error handling in Express
Normally you'll have routes, possibly with further nested routes, but the process for handling errors will be exactly the same no matter ...
Read more >
Nested Middleware - make the inside Middleware an exception
Not sure if it's possible. But let's say I have a big group of routes that all uses some middleware with specific value:...
Read more >
Express.js Middleware Can Be Arbitrarily Nested Within A ...
Since an error handler is just a middleware with a special invocation signature (4 arguments exactly), each route can have its own error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found