reactjs NextJS构建错误:Edge中不允许动态代码评估(例如“eval”、“new Function'、”WebAssembly.compile')

9wbgstp7  于 2023-11-18  发布在  React
关注(0)|答案(1)|浏览(122)

我无法构建我的项目,它是一个带有NextJS的React,带有中间件,只是一个前端消耗后端。
这是我的package.json

{
  "name": "xxxx",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "format": "prettier --write \"{components,app,service,api-generator}/**/*.{js,ts,tsx,d.ts}\"",
    "lint": "next lint"
  },
  "dependencies": {
    "@react-google-maps/api": "^2.19.2",
    "@types/node": "20.3.1",
    "@types/react": "18.2.12",
    "@types/react-dom": "18.2.5",
    "chart.js": "4.2.1",
    "next": "13.5.6",
    "next-auth": "^4.24.4",
    "primeflex": "^3.3.1",
    "primeicons": "^6.0.1",
    "primereact": "^9.6.2",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-icons": "^4.10.1",
    "typescript": "5.1.3"
  },
  "devDependencies": {
    "eslint": "8.43.0",
    "eslint-config-next": "13.4.6",
    "prettier": "^2.8.8",
    "sass": "^1.63.4"
  }
}

字符串
当我尝试构建时的错误:

Failed to compile.

./node_modules/@babel/runtime/regenerator/index.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime
Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation

Import trace for requested module:
./node_modules/@babel/runtime/regenerator/index.js
./node_modules/next-auth/react/index.js

> Build failed because of webpack errors
   Creating an optimized production build  .


我不知道该怎么办,有人能帮忙吗?

bis0qfac

bis0qfac1#

问题是在我的midware.ts文件,当我得到的会话,以确定是否有一些用户登录,所以我可以重定向它或没有.为了解决我这样做:

import { NextRequest, NextResponse } from 'next/server';
//import { getSession } from 'next-auth/react';
import { getToken } from 'next-auth/jwt';

export const config = {
    matcher: ['/', '/blocks/:path*', '/documentation/:path*', '/pages/:path*', '/uikit/:path*', '/utilities/:path*']
};

export default async function middleware(req: NextRequest, res: NextResponse) {
    const signInPage = '/auth/login';

    const requestForNextAuth: any = {
        headers: {
            cookie: req.headers.get('cookie')
        }
    };

    //const session = await getSession({ req: requestForNextAuth });
    const session = await getToken({ req: req, secret: process.env.NEXTAUTH_SECRET });

    console.log(session);

    if (session) {
        return NextResponse.next();
    }

    const signInUrl = new URL(signInPage, req.nextUrl.origin);

    return NextResponse.redirect(signInUrl);
}

字符串
使用getSession方法构建总是失败,所以将其更改为getToken,构建工作正常。

相关问题