Can't run Express app on Azure App Service with pm2 (CLI not found)

416 views Asked by At

As the title suggests, I'm attempting to deploy an Express API to Azure App Services. I am using Github actions to do the pipeline (connected to my master). I am able to run the project perfect locally, but I am getting the following error with both npm and yarn run start:

Error: Cannot find module '../lib/binaries/CLI.js'
Require stack:
- /home/site/wwwroot/node_modules/.bin/pm2
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:101:18)
    at Object.<anonymous> (/home/site/wwwroot/node_modules/.bin/pm2:3:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/site/wwwroot/node_modules/.bin/pm2' ]
}

The pipeline file for my Github actions look like this (fairly standard). I used Azure's tools to connect this, and the action yml was generated for me as a result. I've just removed the test command from the below.


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

on:
  push:
    branches:
      - master
  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: '18.x'

      - name: npm install, build, and test
        run: |
          npm install
          npm run build --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: 'xxx'
          slot-name: 'Production'
          publish-profile: ${{ secrets.xxxB }}
          package: .

I'm using this boilerplate as my starter (which is great by the way): https://github.com/hagopj13/node-express-boilerplate.git

This is my ecosystem.config.json file (also stock standard):

{
    "apps": [
        {
            "name": "app",
            "script": "src/index.js",
            "instances": 1,
            "autorestart": true,
            "watch": false,
            "time": true,
            "env": {
                "NODE_ENV": "production"
            }
        }
    ]
}

I've matched up my Node environments (both on 18.16). I've also tried uninstalling all my modules and deleting the package lock file via Kudu. I've tried reinstalling pm2 on Kudu. I've tried reinstalling the entire project manually. None of it worked for me.

Coincidentally I am getting an error with the cross-env package when I try run npm run dev on Kudu as well.

internal/modules/cjs/loader.js:905
  throw err;
  ^

Error: Cannot find module '..'
Require stack:
- /home/site/wwwroot/node_modules/.bin/cross-env
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:101:18)
    at Object.<anonymous> (/home/site/wwwroot/node_modules/.bin/cross-env:3:18)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/site/wwwroot/node_modules/.bin/cross-env' ]
}

I'm wondering if maybe this is related to perhaps the path of my modules, but I am not an expert by any stretch when it comes to Azure and really a bit lost on how to further troubleshoot this. Would appreciate any ideas!

1

There are 1 answers

0
SiddheshDesai On

I tried to deploy Express.js app with pm2 module and it was deployed and ran successfully in azure web app with Node 18.

I referred this Quickstart in addition I added the below command to install pm2 in as node modules.

npm install pm2

My package.json:-

{
  "name": "myexpressapp7",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "ejs": "~2.6.1",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "morgan": "~1.9.1",
    "pm2": "^5.3.0"
  }
}

My Node modules dependencies:-

enter image description here

My express app directory structure:-

enter image description here

I pushed the above code in my github repository and ran the github action workflow like below:-

Selected Deployment > Deployment Center> Added my github repository which has my express app code and started the deployment:-

enter image description here

My github workflow:-

You can find my complete workflow code here.

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

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

My github Action workflow ran successfully:-

enter image description here

I edited the start up command to run pm2 and the pm2 started successfully, Refer below:-

pm2 start  ./bin/www

enter image description here

enter image description here