【问题标题】:Next.js13 MiddleWare occurs [TypeError: Class extends value undefined is not a constructor or null]next.js13 MiddleWare出现[TypeError: Class extends value undefined is not a constructor or null]
【发布时间】:2022-12-20 07:07:29
【问题描述】:

我的 Next.js 项目尝试中间件到 ver13 时出现 [TypeError: Class extends value undefined is not a constructor or null]。 但是当我尝试 ver12.那没有错误并且工作正常。 有人可以帮助我吗?

/middleware.ts

import { NextRequest, NextResponse } from 'next/server'

export const config = {
  matcher: ['/', '/index'],
}

export function middleware(req: NextRequest) {
  const basicAuth = req.headers.get('authorization')
  const url = req.nextUrl

  if (basicAuth) {
    const authValue = basicAuth.split(' ')[1]
    const [user, pwd] = atob(authValue).split(':')

    if (user === 'username' && pwd === 'password') {
      return NextResponse.next()
    }
  }
  url.pathname = '/api/auth'

  return NextResponse.rewrite(url)
}

/pages/api/auth.ts

import type { NextApiRequest, NextApiResponse } from 'next'

export default function handler(_: NextApiRequest, res: NextApiResponse) {
  res.setHeader('WWW-authenticate', 'Basic realm="Secure Area"')
  res.statusCode = 401
  res.end(`Auth Required.`)
}

终端

error - Error [TypeError]: Class extends value undefined is not a constructor or null
    at ../../node_modules/.pnpm/undici@5.11.0/node_modules/undici/lib/fetch/file.js (evalmachine.<anonymous>:5724:19)
    at __require (evalmachine.<anonymous>:14:50)
    at ../../node_modules/.pnpm/undici@5.11.0/node_modules/undici/lib/fetch/formdata.js (evalmachine.<anonymous>:5881:49)
    at __require (evalmachine.<anonymous>:14:50)
    at ../../node_modules/.pnpm/undici@5.11.0/node_modules/undici/lib/fetch/body.js (evalmachine.<anonymous>:6094:35)
    at __require (evalmachine.<anonymous>:14:50)
    at ../../node_modules/.pnpm/undici@5.11.0/node_modules/undici/lib/fetch/response.js (evalmachine.<anonymous>:6510:49)
    at __require (evalmachine.<anonymous>:14:50)
    at <unknown> (evalmachine.<anonymous>:11635:30)
    at requireFn (file:///Users/tenrashuuji/projects/LyricMob_Next.js/node_modules/next/dist/compiled/edge-runtime/index.js:1:7079) {
  middleware: true
}

我尝试使用相同的代码运行 Next.js ver12。

【问题讨论】:

    标签: next.js13


    【解决方案1】:

    我自己遇到了这个问题,在这里找到了答案https://github.com/redwoodjs/redwood/issues/6338

    TLDR;

    升级到节点版本 16*

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 2020-08-27
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 2022-12-02
      • 2021-11-02
      • 2017-08-27
      相关资源
      最近更新 更多