You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Next.js单仓应用部署至Azure App Service后无法执行npm start

单仓库Next.js应用部署至Linux Azure App Service启动失败问题排查与解决

我正尝试将单仓库(mono repo)中的Next.js应用部署至Linux版Azure App Service,但仅部分成功。包已部署在Data/SitePackages目录下,但启动命令npm start无法启动应用。App Service配置中设置了WEBSITE_RUN_FROM_PACKAGE=1,服务器似乎无法找到node_modules的引用。是否需要在dist/apps/xxx目录下执行npm install安装node_modules后再部署到Azure?恳请提供解决该问题的建议。

错误信息

2022-12-08T05:49:36.979869309Z PATH="$PATH:/home/site/wwwroot" next start
2022-12-08T05:49:36.987850176Z /opt/startup/startup.sh: 11: /opt/startup/startup.sh: next: not found

发布流水线脚本

- task: Bash@3
        displayName: 'Build'
        inputs:
          targetType: 'inline'
          workingDirectory: $(WorkingDir)
          script: |
            config="development"
            npx nx affected --target=build --configuration $config --parallel --maxParallel=10 
            --base=$(Base)
        continueOnError: false
- task: ArchiveFiles@2
        inputs:
          rootFolderOrFile: '$(Build.SourcesDirectory)/dist/apps/xxx'
          archiveFile: '$(Build.SourcesDirectory)/dist/apps/xxx.zip'
          includeRootFolder: false
        enabled: true
- task: AzureRmWebAppDeployment@4
       displayName: 'Deploy'
       inputs:
          ConnectionType: AzureRM
          azureSubscription: 'xxx-xxx-xxx-contributor'
          appType: webApp
          ResourceGroupName: 'xx-eus2-dev-xxx-appqxxx-rg'
          WebAppName: 'xxx-xxx-webapp'
          package:  '$(Build.SourcesDirectory)/dist/apps/xxx.zip'
          DeployToSlotOrASEFlag: true
          SlotName: DEV
          StartupCommand: 'npm start'
      continueOnError: false

Azure日志流

/home/LogFiles/2022_12_08_ln0sdlwk00000B_default_docker.log
2022-12-08T05:49:36.979836609Z cd "/home/site/wwwroot"
2022-12-08T05:49:36.979841809Z 
2022-12-08T05:49:36.979846709Z export NODE_PATH=/usr/local/lib/node_modules:$NODE_PATH
2022-12-08T05:49:36.979851109Z if [ -z "$PORT" ]; then
2022-12-08T05:49:36.979855809Z      export PORT=8080
2022-12-08T05:49:36.979860309Z fi
2022-12-08T05:49:36.979865109Z 
2022-12-08T05:49:36.979869309Z PATH="$PATH:/home/site/wwwroot" next start
2022-12-08T05:49:36.987850176Z /opt/startup/startup.sh: 11: /opt/startup/startup.sh: next: not found

2022-12-08T05:49:40.728Z INFO  - Container xxx-xxx-webapp_1_7deaa4c2_middleware for site xx-xxx-webapp initialized successfully and is ready to serve requests.
2022-12-08T05:49:40.739Z ERROR - Container xxx-xxx-webapp_1_7deaa4c2 didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.
2022-12-08T05:49:40.744Z INFO  - Stopping site xxx-xxx-webapp because it failed during startup.

构建后应用的Package.json

"scripts": {
   "start": "next start"
 }

Azure AppService配置

配置中已设置WEBSITE_RUN_FROM_PACKAGE=1,同时配置了Node.js相关环境变量(如NODE_VERSION)。


问题分析与解决建议

核心原因

错误next: not found的本质是:部署的包中没有包含node_modules目录,而next start命令依赖本地安装的next包;同时WEBSITE_RUN_FROM_PACKAGE=1启用了只读文件系统,无法在部署后执行npm install来补充依赖。

具体解决方案

方案1:构建时打包生产依赖(推荐)

修改流水线,在构建前安装依赖,并确保构建产物包含node_modules:

  1. 更新Build任务,增加依赖安装步骤:
- task: Bash@3
  displayName: 'Install Dependencies & Build'
  inputs:
    targetType: 'inline'
    workingDirectory: $(WorkingDir)
    script: |
      config="production"  # 建议使用生产环境配置,减少冗余依赖
      # 针对mono repo,可指定工作区安装依赖
      npm install --workspace apps/xxx
      npx nx affected --target=build --configuration $config --parallel --maxParallel=10 --base=$(Base)
      # 可选:清理开发依赖,减小包体积
      npm prune --production --workspace apps/xxx
  1. 确认dist/apps/xxx目录包含node_modules后,重新打包部署。

方案2:使用npx调用next(临时应急)

修改启动命令为使用npx拉取对应版本的next:

StartupCommand: 'npx next start'

注意:这种方式可能因版本不匹配引发兼容性问题,仅作为临时方案使用。

方案3:禁用只读模式(不推荐)

若必须在部署后安装依赖,可关闭WEBSITE_RUN_FROM_PACKAGE=1,并修改启动命令:

StartupCommand: 'npm install && npm start'

弊端:每次启动都会重新安装依赖,启动速度慢,且禁用只读模式可能降低部署稳定性。

额外优化点

  • 始终使用production配置构建,避免打包开发环境的冗余代码与依赖
  • 利用NX的deploy目标简化单仓库应用的部署流程,确保依赖与构建产物的一致性

内容的提问来源于stack exchange,提问作者user2696668

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 07:50:46