简体   繁体   中英

package.json issue while deploying a node api on Azure App Service

I wish to deploy a node api on Azure App Service using Github Actions. The app I wish to deploy is on a subfolder called "api" of the repository.

The Github Actions part works fine, but the website doesn't work, and when I look in the logs, it shows this error: "Error: Cannot find module '../package.json'".

The app should look for package.json in./, I don't know why it is searching in../.

Here is my Github Actions yaml:

# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
# More GitHub Actions for Azure: https://github.com/Azure/actions

name: Build and deploy Node.js app to Azure Web App - hobbeez

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
        working-directory: api

      - 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: 'hobbeez'
          slot-name: 'Production'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_16C52153CB9348578BF747E038921ED6 }}
          package: .

I added a ".deployment" file at the root of the repository to specify that it should deploy the "api" subfolder, here is what this file contains:

[config]
project = api

I tried to search for the answer for weeks, but I didn't manage to find a solution. Does anyone have an idea of what am I doing wrong?

Thanks in advance.

  • I have deployed a node api on Azure App Service using GitHubActions
  • Your source file index.js or app.js file has to be under src folder在此处输入图像描述
  • In package.json , add the start script as index.js or app.js based on your script
"start": "node src/index.js",

在此处输入图像描述

 [config] project = api

Yes, the way you specified is correct to deploy a subfolder.

  • Another option to deploy the contents of subfolder is by creating a new repository in the subfolder and deploy using Azure CI/CD.

Cannot find module '../package.json

Remove the node_modules folder and package-lock.json and run npm install again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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