You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I create the following middlerware.ts with contents:
import "server-only";
export function middleware() {}
I get the following error:
Server Error
Error: This module cannot be imported from a Client Component module. It should only be used from a Server Component.
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
<unknown>
node_modules/server-only/index.js (1:0)
eval
node_modules/server-only/index.js (1:6)
(middleware)/./node_modules/server-only/index.js
file:///Users/adriang/code/mid-test/.next/server/middleware.js (435:1)
__webpack_require__
file:///Users/adriang/code/mid-test/.next/server/edge-runtime-webpack.js (37:33)
fn
file:///Users/adriang/code/mid-test/.next/server/edge-runtime-webpack.js (268:21)
eval
webpack-internal:///(middleware)/./middleware.ts (5:69)
(middleware)/./middleware.ts
file:///Users/adriang/code/mid-test/.next/server/middleware.js (29:1)
__webpack_require__
file:///Users/adriang/code/mid-test/.next/server/edge-runtime-webpack.js (37:33)
fn
file:///Users/adriang/code/mid-test/.next/server/edge-runtime-webpack.js (268:21)
eval
webpack-internal:///(middleware)/./node_modules/next/dist/build/webpack/loaders/next-middleware-loader.js (7:72)
(middleware)/./node_modules/next/dist/build/webpack/loaders/next-middleware-loader.js?absolutePagePath=%2FUsers%2Fadriang%2Fcode%2Fmid-test%2Fmiddleware.ts&page=%2Fmiddleware&rootDir=%2FUsers%2Fadriang%2Fcode%2Fmid-test&matchers=&preferredRegion=&middlewareConfig=e30%3D!
file:///Users/adriang/code/mid-test/.next/server/middleware.js (18:1)
Current vs. Expected behavior
The expectation is that this would not throw the above exception since I am under the impression that middleware would only run on the server.
It doesn't make sense to run them client-side, from docs:
Middleware allows you to run code before a request is completed. Then, based on the incoming request, you can modify the response by rewriting, redirecting, modifying the request or response headers, or responding directly.
Verify canary release
I verified that the issue exists in the latest Next.js canary release
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.
Link to the code that reproduces this issue or a replay of the bug
https://codesandbox.io/p/sandbox/vigorous-ace-sx277k?file=/middleware.ts:1,1
To Reproduce
If I create the following
middlerware.ts
with contents:I get the following error:
Current vs. Expected behavior
The expectation is that this would not throw the above exception since I am under the impression that middleware would only run on the server.
It doesn't make sense to run them client-side, from docs:
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 22.6.0: Wed Jul 5 22:21:53 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6020 Binaries: Node: 18.16.0 npm: 9.5.1 Yarn: N/A pnpm: N/A Relevant Packages: next: 13.4.12 eslint-config-next: 13.4.12 react: 18.2.0 react-dom: 18.2.0 typescript: 5.1.6 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Middleware / Edge (API routes, runtime)
Additional context
No response
The text was updated successfully, but these errors were encountered: