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
This is what my terminal spits out when I
az webapp log tail
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:-
My package.json:-
node modules folder contains express package:-
My github workflow:-
Output:-
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:-