简体   繁体   English

在 Azure 应用服务上部署 NextJS 应用后无法运行 npm 启动

[英]Unable to run npm start after deploying NextJS app on Azure App Service

I am trying to deploy Next.js app from a mono repo to azure appservice (linux) but with partial success.我正在尝试将 Next.js 应用程序从 mono 存储库部署到 azure appservice (linux),但部分成功。 While I can see the package deployed in Data/SitePackages but startup command (npm start) fail to start the application.虽然我可以看到 package 部署在 Data/SitePackages 中,但启动命令 (npm start) 无法启动应用程序。 Config settings for the appservice has WEBSITE_RUN_FROM_PACKAGE=1.应用服务的配置设置有 WEBSITE_RUN_FROM_PACKAGE=1。 It appears that server has no reference to node_modules.服务器似乎没有引用 node_modules。 Do i need to do npm install in the dist/apps/xxx folder for node_modules then deploy to azure?.我是否需要在 node_modules 的 dist/apps/xxx 文件夹中执行 npm 安装,然后部署到 azure? Appreciate your inputs for solving the issue感谢您为解决问题提供的意见

Error:错误:

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

Release pipeline script发布流水线脚本

- 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 Log Stream Azure 日志 Stream

/home/LogFiles/2022_12_08_ln0sdlwk00000B_default_docker.log  (https://xxx-xxx- 
webapp.scm.azurewebsites.net/api/vfs/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 of the app after the build构建后应用程序的Package.json

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

Azure AppService Configuration Azure 应用服务配置在此处输入图像描述

  • Here using this yaml file and I was able to deploy the nextjs app to azure app service.在这里使用这个 yaml 文件,我能够将 nextjs 应用程序部署到 azure 应用程序服务。
name: Build and deploy Node.js app to Azure Web App 

on:
    push:
        branches:
            - main
    workflow_dispatch:
    
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v2
            - name: Set up Node.js version
              uses: actions/setup-node@v1
              with:
                node-version: '16.x'
            - name: npm install, build, and test
              run: |
                npm install
                npm run build --if-present
                npm run test --if-present

            - name: Upload artifact for deployment job
              uses: actions/upload-artifact@v2
              with:
                name: node-app
                path: .
                
        deploy:
            runs-on: ubuntu-latest
            needs: build
            environment:    
                name: 'Production'
                url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
            steps:
                - name: Download artifact from build job
                  uses: actions/download-artifact@v2
                  with:
                    name: node-app
                    
                - name: 'Deploy to Azure Web App'
                  id: deploy-to-webapp
                  uses: azure/webapps-deploy@v2
                  with:
                    app-name: 'v-mganorkarnext'
                    slot-name: 'Production'
                    publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_<pulblish profile>}}

                    package: .

在此处输入图像描述

This way there won't be a need to add startup commands and even if you want to run them first run npm install then npm run build and then next start这样就不需要添加启动命令,即使你想运行它们先运行npm install然后npm run build然后next start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM