初始化Next.js TypeScript空项目时出现TypeError报错求助
Next.js TypeScript 空项目初始化后出现 TypeError 错误
刚初始化Next.js TypeScript项目,哪怕是空项目,也触发了未知错误,具体报错信息如下:
TypeError: responseHeaders.getSetCookie 不是一个函数 在 new ResponseCookies (webpack-internal:///(app-metadata-route)/./node_modules/next/dist/compiled/@edge-runtime/cookies/index.js:213:47) 在 new NextResponse (webpack-internal:///(app-metadata-route)/./node_modules/next/dist/server/web/spec-extension/response.js:40:22) 在 GET (webpack-internal:///(app-metadata-route)/./node_modules/next/dist/build/webpack/loaders/next-metadata-route-loader.js?page=%2Ffavicon.ico%2Froute&isDynamic=0!./src/app/favicon.ico?__next_metadata_route__:15:10) 在 G:\groove-hub\node_modules\next\dist\compiled\next-server\app-route.runtime.dev.js:6:62505 在 G:\groove-hub\node_modules\next\dist\server\lib\trace\tracer.js:121:36 在 NoopContextManager.with (G:\groove-hub\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:7057) 在 ContextAPI.with (G:\groove-hub\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:516) 在 NoopTracer.startActiveSpan (G:\groove-hub\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:18086) 在 ProxyTracer.startActiveSpan (G:\groove-hub\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:18847) 在 G:\groove-hub\node_modules\next\dist\server\lib\trace\tracer.js:110:107 在 NoopContextManager.with (G:\groove-hub\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:7057) 在 ContextAPI.with (G:\groove-hub\node_modules\next\dist\compiled\@opentelemetry\api\index.js:1:516) 在 NextTracerImpl.trace (G:\groove-hub\node_modules\next\dist\server\lib\trace\tracer.js:110:32) 在 G:\groove-hub\node_modules\next\dist\compiled\next-server\app-route.runtime.dev.js:6:56147 在 AsyncLocalStorage.run (node:async_hooks:330:14) 在 Object.wrap (G:\groove-hub\node_modules\next\dist\compiled\next-server\app-route.runtime.dev.js:6:37057) 在 G:\groove-hub\node_modules\next\dist\compiled\next-server\app-route.runtime.dev.js:6:53934 在 AsyncLocalStorage.run (node:async_hooks:330:14) 在 Object.wrap (G:\groove-hub\node_modules\next\dist\compiled\next-server\app-route.runtime.dev.js:6:36635) 在 G:\groove-hub\node_modules\next\dist\compiled\next-server\app-route.runtime.dev.js:6:53896 在 AsyncLocalStorage.run (node:async_hooks:330:14) 在 eD.execute (G:\groove-hub\node_modules\next\dist\compiled\next-server\app-route.runtime.dev.js:6:53870) 在 eD.handle (G:\groove-hub\node_modules\next\dist\compiled\next-server\app-route.runtime.dev.js:6:63748) 在 doRender (G:\groove-hub\node_modules\next\dist\server\base-server.js:1217:56) 在 cacheEntry.responseCache.get.incrementalCache.incrementalCache (G:\groove-hub\node_modules\next\dist\server\base-server.js:1446:34) 在 ResponseCache.get (G:\groove-hub\node_modules\next\dist\server\response-cache\index.js:53:26) 在 DevServer.renderToResponseWithComponentsImpl (G:\groove-hub\node_modules\next\dist\server\base-server.js:1361:53) 在 G:\groove-hub\node_modules\next\dist\server\base-server.js:903:121 在 NextTracerImpl.trace (G:\groove-hub\node_modules\next\dist\server\lib\trace\tracer.js:94:20) 在 DevServer.renderToResponseWithComponents (G:\groove-hub\node_modules\next\dist\server\base-server.js:903:41) 在 DevServer.renderPageComponent (G:\groove-hub\node_modules\next\dist\server\base-server.js:1600:35) 在 async DevServer.renderToResponseImpl (G:\groove-hub\node_modules\next\dist\server\base-server.js:1638:32) 在 async DevServer.pipeImpl (G:\groove-hub\node_modules\next\dist\server\base-server.js:826:25) 在 async DevServer.handleCatchallRenderRequest (G:\groove-hub\node_modules\next\dist\server\next-server.js:623:13) 在 async DevServer.handleRequestImpl (G:\groove-hub\node_modules\next\dist\server\base-server.js:728:17)
解决方法
- 升级Node.js版本:这个错误核心原因是Node.js版本过低,
getSetCookie是Node.js 18+才支持的Response Headers方法,确保你的Node.js版本在18.17.0或以上(Next.js 13+官方推荐版本)。 - 清除缓存并重装依赖:
- 删除
node_modules文件夹:rm -rf node_modules(Windows系统用rmdir /s /q node_modules) - 删除
package-lock.json或yarn.lock文件 - 重新安装依赖:
npm install或yarn install
- 删除
- 升级Next.js到稳定版:避免使用beta/rc版本,执行
npm install next@latest升级到最新稳定版。 - 临时处理favicon文件:从报错栈可以看到问题触发于
favicon.ico的元数据路由处理,尝试删除src/app/favicon.ico文件,重启项目后再重新添加。
内容的提问来源于stack exchange,提问作者Shubham Kharade
相关产品推荐
相关产品推荐

