Amplify部署Next.js应用报TypeError: handler is not a function但本地正常
Next.js 13.4 + Amplify 部署 "handler is not a function" 错误解决方法
问题概述
使用Next.js 13.4、Node 18.16构建的应用本地运行正常,但部署到Amplify后访问报错,CloudWatch显示错误:
TypeError: handler is not a function
at Server.(/tmp/app/server.js:29:11)
at Server.emit (node:events:513:28)
at parserOnIncoming (node:_http_server:998:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
已尝试使用public.ecr.aws/docker/library/node:18.16.0镜像,且默认Next.js应用部署也出现相同问题。
解决步骤
配置Next.js输出模式
在next.config.js中添加output: 'standalone',生成适配服务器运行的独立包:/** @type {import('next').NextConfig} */ const nextConfig = { output: 'standalone', } module.exports = nextConfig自定义Amplify构建配置
项目根目录创建amplify.yml,指定Node版本和构建流程:version: 1 frontend: phases: preBuild: commands: - nvm install 18.16.0 - nvm use 18.16.0 - npm install build: commands: - npm run build artifacts: baseDirectory: .next files: - '**/*' cache: paths: - node_modules/**/*验证Lambda运行时版本
在Amplify控制台的部署设置中,确认Lambda运行时为Node.js 18.x,若不符则手动调整。清理缓存并重新部署
在Amplify控制台触发重新部署,勾选"清理构建缓存"选项,避免旧文件干扰。
内容的提问来源于stack exchange,提问作者100tifiko
相关产品推荐
相关产品推荐

