部署Next.js应用至AWS Amplify时遇Cannot read 'next'版本错误
问题:AWS Amplify部署Next.js应用时提示无法读取package.json中的next版本
部署纯前端Next.js应用到AWS Amplify时,构建阶段持续报错:
2023-05-20T17:00:51.907Z [INFO]: # Checking for Git submodules at: /codebuild/output/src963349001/src/static/.gitmodules 2023-05-20T17:00:51.911Z [ERROR]: !!! CustomerError: Cannot read 'next' version in package.json.
但package.json中已明确包含next依赖:
{ "name": "one-two-static", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build && next export", "start": "next start" }, "dependencies": { "@types/node": "20.2.1", "@types/react": "18.2.6", "@types/react-dom": "18.2.4", "autoprefixer": "10.4.14", "eslint": "8.41.0", "eslint-config-next": "13.4.3", "next": "13.4.3", "postcss": "8.4.23", "react": "18.2.0", "react-dom": "18.2.0", "tailwindcss": "3.3.2", "typescript": "5.0.4" } }
当前next.config.js配置:
/** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, output: 'standalone', experimental: { outputStandalone: true, } } module.exports = nextConfig
解决方法
检查Amplify构建的根目录路径:日志显示检查路径为
/codebuild/output/src963349001/src/static/.gitmodules,可能Amplify未指向包含package.json的项目根目录。在Amplify控制台的构建设置中,确认「App root directory」是否正确,若package.json在子目录需调整路径。移除重复的Next.js输出配置:Next.js 13+只需保留
output: 'standalone'即可,experimental.outputStandalone是旧版本配置,重复设置可能引发构建逻辑冲突。修改后的next.config.js:/** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, output: 'standalone', } module.exports = nextConfig清理Amplify构建缓存:进入Amplify控制台的应用构建页面,点击「Redeploy this version」并勾选「Clear build cache」,重新触发构建,避免旧缓存干扰。
确认Git提交完整性:确保package.json已正确提交至Git仓库,未被.gitignore规则忽略,保证Amplify拉取代码时能获取完整的package.json文件。
内容的提问来源于stack exchange,提问作者code writer 3000
相关产品推荐
相关产品推荐

