通过GitHub Actions部署Next.js到Azure Web App时遇构建文件缺失错误
未启用Next.js Standalone模式
你的工作流依赖.next/standalone目录,但如果没有在next.config.js中配置output: 'standalone',Next.js不会生成这个目录。此时npm run build只会生成常规的.next目录,导致后续复制命令失败、上传的工件为空,部署后自然找不到.next目录。
解决:在next.config.js中添加如下配置:/** @type {import('next').NextConfig} */ const nextConfig = { output: 'standalone', } module.exports = nextConfig工件上传内容不完整
你仅上传了.next/standalone,但Standalone模式下,部署还需要public文件夹(如果项目有)、.next/static目录以及根目录的package.json,否则Next.js无法找到静态资源和必要构建文件。
解决:修改上传工件的步骤,调整路径配置:- name: Upload artifact for deployment job uses: actions/upload-artifact@v4 with: name: app path: | .next/standalone .next/static public package.json启动命令与部署目录结构不匹配
Azure Web App的默认工作目录是site/wwwroot,如果仅上传.next/standalone,部署后server.js在wwwroot下,但.next目录不在同级(仅存在于standalone/.next),导致node server.js执行时找不到./.next路径。
解决:调整复制静态文件的逻辑,将.next的必要内容复制到standalone的同级目录,确保部署后wwwroot下同时存在server.js和.next目录。静态文件复制命令执行失败
若未启用Standalone模式,.next/standalone目录不存在,cp -R ./.next/static ./.next/standalone/.next/static命令会直接失败,导致静态资源未被纳入工件,部署后缺少.next目录内容。
解决:先确保Standalone模式已启用,再执行复制命令,或添加存在性判断:if [ -d ./.next/standalone ]; then mkdir -p ./.next/standalone/.next/static cp -R ./.next/static ./.next/standalone/.next/static fi
内容的提问来源于stack exchange,提问作者eecan

