Module not found 'express' when deploying to azure app service

huangapple go评论123阅读模式
英文:

Module not found 'express' when deploying to azure app service

问题

以下是您提供的内容的翻译:

  1. 我有一个名为 server.js 的节点文件我可以在本地成功运行它它的代码如下
  2. const express = require('express')
  3. const compression = require('compression')
  4. const path = require('path')
  5. const { renderPage } = require('vite-plugin-ssr/server')
  6. const isProduction = process.env.NODE_ENV === 'production'
  7. const root = path.resolve(__dirname, '..')
  8. const clientPath = path.join(root, 'client')
  9. async function startServer() {
  10. const app = express()
  11. app.use(compression())
  12. if (isProduction) {
  13. const sirv = require('sirv')
  14. app.use(sirv(clientPath))
  15. } else {
  16. const vite = require('vite')
  17. const viteDevMiddleware = (
  18. await vite.createServer({
  19. root,
  20. server: { middlewareMode: true },
  21. })
  22. ).middlewares
  23. app.use(viteDevMiddleware)
  24. }
  25. // eslint-disable-next-line consistent-return
  26. app.get('*', async (req, res, next) => {
  27. const pageContextInit = {
  28. urlOriginal: req.originalUrl,
  29. }
  30. const pageContext = await renderPage(pageContextInit)
  31. const { httpResponse } = pageContext
  32. if (!httpResponse) return next()
  33. const {
  34. body, statusCode, contentType, earlyHints,
  35. } = httpResponse
  36. if (res.writeEarlyHints) res.writeEarlyHints({ link: earlyHints.map((e) => e.earlyHintLink) })
  37. res.status(statusCode).type(contentType).send(body)
  38. })
  39. const port = process.env.PORT || 5173
  40. app.listen(port)
  41. console.log(`Server running at http://localhost:${port}`)
  42. }
  43. startServer()

我还可以在运行 npm run build 后从我的 dist 文件夹中提供服务,没有任何问题。但是,一旦我将其部署到 Azure 的应用服务并尝试在那里运行它,我就会遇到错误。这是我的应用服务启动命令:

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

我的流水线看起来像这样:

  1. name: Azure dev deployment
  2. on:
  3. push:
  4. branches:
  5. - main
  6. env:
  7. AZURE_WEBAPP_NAME: {name}
  8. AZURE_WEBAPP_PACKAGE_PATH: './dist'
  9. NODE_VERSION: '16.x'
  10. jobs:
  11. build-and-deploy:
  12. runs-on: ubuntu-latest
  13. container: node:16
  14. steps:
  15. - uses: actions/checkout@v2
  16. - name: set script to remove husky in CI
  17. run: npm set-script prepare ''
  18. - name: Dependency installation
  19. run: npm ci
  20. - name: linter
  21. run: npm run lint
  22. - name: build
  23. env:
  24. DISABLE_ESLINT_PLUGIN: true
  25. run: npm run build --if-present
  26. - name: tests
  27. env:
  28. DISABLE_ESLINT_PLUGIN: true
  29. run: npm test
  30. - name: 'Deploy to Azure WebApp'
  31. uses: azure/webapps-deploy@v2
  32. with:
  33. app-name: ${{ appName }}
  34. publish-profile: ${{ publishProfile }}

在我的部署包中是否应包含 node_modules 和/或 package.json?我有点迷失方向,正在猜测为什么会出现此错误,我会感激任何帮助。

谢谢!

  1. <details>
  2. <summary>英文:</summary>
  3. I have a node server.js file which i can run just fine locally, and it looks like this:
  4. const express = require(&#39;express&#39;)
  5. const compression = require(&#39;compression&#39;)
  6. const path = require(&#39;path&#39;)
  7. const { renderPage } = require(&#39;vite-plugin-ssr/server&#39;)
  8. const isProduction = process.env.NODE_ENV === &#39;production&#39;
  9. const root = path.resolve(__dirname, &#39;..&#39;)
  10. const clientPath = path.join(root, &#39;client&#39;)
  11. async function startServer() {
  12. const app = express()
  13. app.use(compression())
  14. if (isProduction) {
  15. const sirv = require(&#39;sirv&#39;)
  16. app.use(sirv(clientPath))
  17. } else {
  18. const vite = require(&#39;vite&#39;)
  19. const viteDevMiddleware = (
  20. await vite.createServer({
  21. root,
  22. server: { middlewareMode: true },
  23. })
  24. ).middlewares
  25. app.use(viteDevMiddleware)
  26. }
  27. // eslint-disable-next-line consistent-return
  28. app.get(&#39;*&#39;, async (req, res, next) =&gt; {
  29. const pageContextInit = {
  30. urlOriginal: req.originalUrl,
  31. }
  32. const pageContext = await renderPage(pageContextInit)
  33. const { httpResponse } = pageContext
  34. if (!httpResponse) return next()
  35. const {
  36. body, statusCode, contentType, earlyHints,
  37. } = httpResponse
  38. if (res.writeEarlyHints) res.writeEarlyHints({ link: earlyHints.map((e) =&gt; e.earlyHintLink) })
  39. res.status(statusCode).type(contentType).send(body)
  40. })
  41. const port = process.env.PORT || 5173
  42. app.listen(port)
  43. console.log(`Server running at http://localhost:${port}`)
  44. }
  45. startServer()
  46. 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:
  47. pm2 start js/server.js --no-daemon
  48. My pipeline looks like this:
  49. name: Azure dev deployment
  50. on:
  51. push:
  52. branches:
  53. - main
  54. env:
  55. AZURE_WEBAPP_NAME: {name}
  56. AZURE_WEBAPP_PACKAGE_PATH: &#39;./dist&#39;
  57. NODE_VERSION: &#39;16.x&#39;
  58. jobs:
  59. build-and-deploy:
  60. runs-on: ubuntu-latest
  61. container: node:16
  62. steps:
  63. - uses: actions/checkout@v2
  64. - name: set script to remove husky in CI
  65. run: npm set-script prepare &#39;&#39;
  66. - name: Dependency installation
  67. run: npm ci
  68. - name: linter
  69. run: npm run lint
  70. - name: build
  71. env:
  72. DISABLE_ESLINT_PLUGIN: true
  73. run: npm run build --if-present
  74. - name: tests
  75. env:
  76. DISABLE_ESLINT_PLUGIN: true
  77. run: npm test
  78. - name: &#39;Deploy to Azure WebApp&#39;
  79. uses: azure/webapps-deploy@v2
  80. with:
  81. app-name: ${{ appName }}
  82. publish-profile: ${{ publishProfile }}
  83. Should node_modules and/or package.json be included in my deployment package to my app service? I&#39;m a bit lost here and trying to speculate as to why I&#39;m getting this error and would appreciate any help.
  84. Thanks
  85. </details>
  86. # 答案1
  87. **得分**: 1
  88. 确保在运行 GitHub Action 工作流的 GitHub 仓库的节点模块中包含 express.js:
  89. **我的 GitHub 仓库包含 Express.js 应用程序:**
  90. ![在此输入图片描述](https://i.imgur.com/yeGhwJ8.png)
  91. **我的 package.json:**
  92. ```json
  93. {
  94. "name": "myexpressapp7",
  95. "version": "0.0.0",
  96. "private": true,
  97. "scripts": {
  98. "start": "node ./bin/www"
  99. },
  100. "dependencies": {
  101. "cookie-parser": "~1.4.4",
  102. "debug": "~2.6.9",
  103. "ejs": "~2.6.1",
  104. "express": "~4.16.1",
  105. "http-errors": "~1.6.3",
  106. "morgan": "~1.9.1",
  107. "pm2": "^5.3.0"
  108. }
  109. }

节点模块文件夹中包含 express 包:

Module not found 'express' when deploying to azure app service

我的 GitHub 工作流:

  1. name: 构建并部署 Node.js 应用程序到 Azure Web App - valleywebapp984
  2. on:
  3. push:
  4. branches:
  5. - master
  6. workflow_dispatch:
  7. jobs:
  8. build:
  9. runs-on: ubuntu-latest
  10. steps:
  11. - uses: actions/checkout@v2
  12. - name: 设置 Node.js 版本
  13. uses: actions/setup-node@v1
  14. with:
  15. node-version: '18.x'
  16. - name: npm 安装,构建和测试
  17. run: |
  18. npm install
  19. npm run build --if-present
  20. npm run test --if-present
  21. - name: 上传部署作业的构件
  22. uses: actions/upload-artifact@v2
  23. with:
  24. name: node-app
  25. path: .
  26. deploy:
  27. runs-on: ubuntu-latest
  28. needs: build
  29. environment:
  30. name: 'production'
  31. url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
  32. steps:
  33. - name: 从构建作业下载构件
  34. uses: actions/download-artifact@v2
  35. with:
  36. name: node-app
  37. - name: '部署到 Azure Web App'
  38. id: deploy-to-webapp
  39. uses: azure/webapps-deploy@v2
  40. with:
  41. app-name: 'valleywebapp984'
  42. slot-name: 'production'
  43. publish-profile: ${{ secrets.AzureAppService_PublishProfile_2eccd9239cc34b3990c26fadf7c6ae60 }}
  44. package: .

输出:

Module not found 'express' when deploying to azure app service

Module not found 'express' when deploying to azure app service

为了运行 pm2 服务器,请参考我的 Stack Overflow 帖子回答 此处

将此作为 Web 应用程序的启动命令添加到 Web 应用程序中,重新加载您的 Web 应用程序并像下面这样检查日志流:

启动命令:

  1. pm2 start ./bin/www

Module not found 'express' when deploying to azure app service

英文:

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:-

Module not found 'express' when deploying to azure app service

My package.json:-

  1. {
  2. &quot;name&quot;: &quot;myexpressapp7&quot;,
  3. &quot;version&quot;: &quot;0.0.0&quot;,
  4. &quot;private&quot;: true,
  5. &quot;scripts&quot;: {
  6. &quot;start&quot;: &quot;node ./bin/www&quot;
  7. },
  8. &quot;dependencies&quot;: {
  9. &quot;cookie-parser&quot;: &quot;~1.4.4&quot;,
  10. &quot;debug&quot;: &quot;~2.6.9&quot;,
  11. &quot;ejs&quot;: &quot;~2.6.1&quot;,
  12. &quot;express&quot;: &quot;~4.16.1&quot;,
  13. &quot;http-errors&quot;: &quot;~1.6.3&quot;,
  14. &quot;morgan&quot;: &quot;~1.9.1&quot;,
  15. &quot;pm2&quot;: &quot;^5.3.0&quot;
  16. }
  17. }

node modules folder contains express package:-

Module not found 'express' when deploying to azure app service

My github workflow:-

  1. name: Build and deploy Node.js app to Azure Web App - valleywebapp984
  2. on:
  3. push:
  4. branches:
  5. - master
  6. workflow_dispatch:
  7. jobs:
  8. build:
  9. runs-on: ubuntu-latest
  10. steps:
  11. - uses: actions/checkout@v2
  12. - name: Set up Node.js version
  13. uses: actions/setup-node@v1
  14. with:
  15. node-version: &#39;18.x&#39;
  16. - name: npm install, build, and test
  17. run: |
  18. npm install
  19. npm run build --if-present
  20. npm run test --if-present
  21. - name: Upload artifact for deployment job
  22. uses: actions/upload-artifact@v2
  23. with:
  24. name: node-app
  25. path: .
  26. deploy:
  27. runs-on: ubuntu-latest
  28. needs: build
  29. environment:
  30. name: &#39;production&#39;
  31. url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
  32. steps:
  33. - name: Download artifact from build job
  34. uses: actions/download-artifact@v2
  35. with:
  36. name: node-app
  37. - name: &#39;Deploy to Azure Web App&#39;
  38. id: deploy-to-webapp
  39. uses: azure/webapps-deploy@v2
  40. with:
  41. app-name: &#39;valleywebapp984&#39;
  42. slot-name: &#39;production&#39;
  43. publish-profile: ${{ secrets.AzureAppService_PublishProfile_2eccd9239cc34b3990c26fadf7c6ae60 }}
  44. package: .

Output:-

Module not found 'express' when deploying to azure app service

Module not found 'express' when deploying to azure app service

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:-

  1. pm2 start ./bin/www

Module not found 'express' when deploying to azure app service

答案2

得分: 0

这是我的终端输出当我运行az webapp log tail时:

英文:

Module not found 'express' when deploying to azure app service

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

huangapple
  • 本文由 发表于 2023年6月28日 23:59:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76574893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定