Next.js应用部署AWS Amplify失败:构建阶段报错求助
Next.js Pages Router部署AWS Amplify构建失败问题
问题描述
使用Pages Router的Next.js项目(含pages文件夹)部署到AWS Amplify时,前端构建阶段失败。日志显示系统尝试查找.next/server/app路径,但项目的.next文件夹结构中不存在该路径(实际为server/pages)。
项目配置文件
next.config.js
/** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, } module.exports = nextConfig
.eslintrc.json
{ "extends": ["next/babel","next/core-web-vitals"] }
package.json
{ "name": "kruegerandco", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "@radix-ui/react-dropdown-menu": "^2.0.5", "@types/node": "20.4.1", "@types/react": "18.2.14", "@types/react-dom": "18.2.6", "autoprefixer": "10.4.14", "class-variance-authority": "^0.6.1", "clsx": "^1.2.1", "eslint": "8.44.0", "eslint-config-next": "13.4.9", "hamburger-react": "^2.5.0", "lucide-react": "^0.259.0", "next": "13.4.9", "postcss": "8.4.25", "react": "18.2.0", "react-dom": "18.2.0", "react-icons": "^4.10.1", "tailwind-merge": "^1.13.2", "tailwindcss": "3.3.2", "tailwindcss-animate": "^1.0.6", "typescript": "5.1.6" } }
amplify.yml
version: 1 frontend: phases: preBuild: commands: - npm ci build: commands: - npm run build artifacts: baseDirectory: .next files: - '**/*' cache: paths: - node_modules/**/*
部署错误日志
2023-07-10T11:45:25.246Z [WARNING]: 2023-07-10T11:45:25.247Z [WARNING]: > Build error occurred 2023-07-10T11:45:25.249Z [WARNING]: [Error: ENOENT: no such file or directory, lstat '/codebuild/output/src923177214/src/kruegerandco/.next/server/app'] { errno: -2, code: 'ENOENT', syscall: 'lstat', path: '/codebuild/output/src923177214/src/kruegerandco/.next/server/app' } 2023-07-10T11:45:25.282Z [ERROR]: !!! Build failed 2023-07-10T11:45:25.282Z [INFO]: Please check the supported SSR features to find if your build failure is related to an unsupported feature: https://docs.aws.amazon.com/amplify/latest/userguide/ssr-Amplify-support.html#supported-unsupported-features. You may also find this troubleshooting guide useful: https://docs.aws.amazon.com/amplify/latest/userguide/troubleshooting-ssr-deployment.html 2023-07-10T11:45:25.283Z [ERROR]: !!! Non-Zero Exit Code detected 2023-07-10T11:45:25.283Z [INFO]: # Starting environment caching... 2023-07-10T11:45:25.283Z [INFO]: # Environment caching completed
.next文件夹结构
项目使用Pages Router,.next文件夹结构如下:
解决方案建议
- 强制指定路由器类型:在amplify.yml的build命令中添加环境变量,强制Next.js使用Pages Router,修改build命令为:
- NEXT_PUBLIC_APP_ROUTER_DISABLED=1 npm run build - 检查项目残留文件:确认项目根目录下没有
app文件夹,若存在则删除,避免Next.js自动切换到App Router模式 - 清理Amplify缓存:在Amplify控制台中触发重新构建并勾选"清空构建缓存"选项,避免旧缓存影响
- 验证amplify.yml配置:确保artifacts的baseDirectory设置正确为
.next,无需额外修改路径,Pages Router的输出会被正确识别
内容的提问来源于stack exchange,提问作者Mossy82
相关产品推荐
相关产品推荐

