Next.js 版本 ^12.1.16 中模块“next”没有导出成员“NextApiRequest”

Chi*_*olo 5 types typescript next.js

当尝试输入版本的 Next.js API 的请求/响应对象时>= 12.1.16,会显示以下错误之一:

Module '"next"' has no exported member 'NextApiRequest'. 
Run Code Online (Sandbox Code Playgroud)

或者

Module '"next"' has no exported member 'NextApiResponse'. 
Run Code Online (Sandbox Code Playgroud)

这在版本中不是问题,< 12.1.16但现在是问题。

如何解决这个问题?

Chi*_*olo 15

要解决此问题,您只需从next/types模块中导入以下版本Next.js > 12.1.16

import type { NextApiRequest, NextApiResponse } from "next/types";
Run Code Online (Sandbox Code Playgroud)

智能感知可能会帮助您解决这个问题,但如果没有,您可以查看该next/types/index.d.ts文件。只要您使用 TypeScript 初始化项目,Next.js 将提供自己的类型,并且它们都可以在该文件中找到 - 每个类型导入都将从该路径进行。

当我提出并回答这个问题时,我在官方文档中找不到任何相关内容。