我剛剛克隆了Next Auth 示例,更改了 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.
}
}
完整日志:
> [email protected] 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! [email protected] build: `next build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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
包.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 因為它對我的環境沒有影響)
是什么導致了問題,我應該如何解決?
uj5u.com熱心網友回復:
NextJS 12 引入了中間件作為一個概念 - 請參見此處 - https://nextjs.org/blog/next-12#introducing-middleware
對于您的用例,請參閱此處并遵循 api 路由示例。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/415175.html
標籤:
