skip to Main Content

I have a node server.js file which i can run just fine locally, and it looks like this:

const express = require('express')
const compression = require('compression')
const path = require('path')
const { renderPage } = require('vite-plugin-ssr/server')

const isProduction = process.env.NODE_ENV === 'production'
const root = path.resolve(__dirname, '..')
const clientPath = path.join(root, 'client')

async function startServer() {
  const app = express()

  app.use(compression())

  if (isProduction) {
    const sirv = require('sirv')
    app.use(sirv(clientPath))
  } else {
    const vite = require('vite')
    const viteDevMiddleware = (
      await vite.createServer({
        root,
        server: { middlewareMode: true },
      })
    ).middlewares
    app.use(viteDevMiddleware)
  }

  // eslint-disable-next-line consistent-return
  app.get('*', async (req, res, next) => {
    const pageContextInit = {
      urlOriginal: req.originalUrl,
    }
    const pageContext = await renderPage(pageContextInit)
    const { httpResponse } = pageContext
    if (!httpResponse) return next()
    const {
      body, statusCode, contentType, earlyHints,
    } = httpResponse
    if (res.writeEarlyHints) res.writeEarlyHints({ link: earlyHints.map((e) => e.earlyHintLink) })
    res.status(statusCode).type(contentType).send(body)
  })

  const port = process.env.PORT || 5173
  app.listen(port)
  console.log(`Server running at http://localhost:${port}`)
}

startServer()

I can also serve it from my dist folder after running npm run build without any issues. But as soon as i deploy it to my app service in azure and try to run it there i get the error. Here is my app service startup command:

pm2 start js/server.js --no-daemon

My pipeline looks like this:

name: Azure dev deployment

on:
  push:
    branches: 
      - main
env:
  AZURE_WEBAPP_NAME: {name}
  AZURE_WEBAPP_PACKAGE_PATH: './dist'
  NODE_VERSION: '16.x'                

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    container: node:16
    steps:
      - uses: actions/checkout@v2
      - name: set script to remove husky in CI
        run: npm set-script prepare ''
      - name: Dependency installation
        run: npm ci
      - name: linter
        run: npm run lint
      - name: build
        env:
          DISABLE_ESLINT_PLUGIN: true
        run: npm run build --if-present
      - name: tests
        env:
          DISABLE_ESLINT_PLUGIN: true
        run: npm test
      - name: 'Deploy to Azure WebApp'
        uses: azure/webapps-deploy@v2
        with: 
          app-name: ${{ appName }}
          publish-profile: ${{ publishProfile }}

Should node_modules and/or package.json be included in my deployment package to my app service? I’m a bit lost here and trying to speculate as to why I’m getting this error and would appreciate any help.

Thanks

2

Answers


  1. Chosen as BEST ANSWER

    enter image description here

    This is what my terminal spits out when I az webapp log tail


  2. Make sure the express.js is included in the node modules in your github repository where you’re running the github action workflow:-

    My github repository with Express.js app:-

    enter image description here

    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"
      }
    }
    

    node modules folder contains express package:-

    enter image description here

    My github workflow:-

    name: Build and deploy Node.js app to Azure Web App - valleywebapp984
    
    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
              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: 'valleywebapp984'
              slot-name: 'production'
              publish-profile: ${{ secrets.AzureAppService_PublishProfile_2eccd9239cc34b3990c26fadf7c6ae60 }}
              package: .
    

    Output:-

    enter image description here

    enter image description here

    In order to run the pm2 server, Refer my SO thread answer here

    Add this as a start up command in the Web app, reload your web app and check the Log stream like below:-

    Start up command:-

    pm2 start  ./bin/www
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search