ModuleNotFoundError: Module not found: Error: Can't resolve 'next/server'

ModuleNotFoundError: Module not found: Error: Can't resolve 'next/server'

我刚刚克隆了 Next Auth Example,更改了 package.json 中的端口以匹配我的服务器并在 .env.local 中添加了我的域。由于 Next 12 中的 WASM 中间件,我不得不降级到 Next 11,所以我只是将 package.json 编辑为 "next": "^11.1.3" 并再次 运行 npm i

当我尝试 npm run build 时,出现此错误:

ModuleNotFoundError: Module not found: Error: Can't resolve 'next/server' in 'domain.com/pages'

npm run dev 工作正常。

具有 server 导入的页面是: https://github.com/nextauthjs/next-auth-example/blob/main/pages/_middleware.js

import { getToken } from "next-auth/jwt"
import { NextResponse } from "next/server"

/** @param {import("next/server").NextRequest} req */
export async function middleware(req) {
  if (req.nextUrl.pathname === "/middleware-protected") {
    const session = await getToken({
      req,
      secret: process.env.SECRET,
      secureCookie:
        process.env.NEXTAUTH_URL?.startsWith("https://") ??
        !!process.env.VERCEL_URL,
    })
    // You could also check for any property on the session object,
    // like role === "admin" or name === "John Doe", etc.
    if (!session) return NextResponse.redirect("/api/auth/signin")
    // If user is authenticated, continue.
  }
}

完整日志:

> next-auth-example@0.0.0 build /domain.com
> next build

info  - Loaded env from /domain.com/.env.local
info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
info  - Checking validity of types
warn  - No ESLint configuration detected. Run next lint to begin setup
info  - Creating an optimized production build
Failed to compile.

ModuleNotFoundError: Module not found: Error: Can't resolve 'next/server' in 'domain.com/pages'


> Build error occurred
Error: > Build failed because of webpack errors
    at domain.com/node_modules/next/dist/build/index.js:397:19
    at async Span.traceAsyncFn (domain.com/node_modules/next/dist/telemetry/trace/trace.js:60:20)
    at async Object.build [as default] (domain.com/node_modules/next/dist/build/index.js:77:25)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! next-auth-example@0.0.0 build: `next build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the next-auth-example@0.0.0 build 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!     domain.com/.npm/_logs/blahblahblah-debug.log // I edited this line

package.json

{
  "name": "authentication",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "author": "damn",
  "license": "ISC",
  "dependencies": {
    "next": "^11.1.2",
    "next-auth": "^4.1.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  }
}

(删除了 -p 因为它对我的环境没有影响)

导致问题的原因是什么,我应该如何解决?

中间件 作为 NextJS 12 引入的概念 - 请参阅此处 - https://nextjs.org/blog/next-12#introducing-middleware

对于您的用例,请参阅 here 并遵循 api 路由示例。