gpt4 book ai didi

node.js - 如何使用部署到 Azure 的应用程序修复 "Error: Cannot find module '/home/site/wwwroot/index.js”?

转载 作者:行者123 更新时间:2023-12-02 07:17:26 26 4
gpt4 key购买 nike

我第一次尝试将node.js应用程序作为Azure应用程序服务部署到Azure,但在尝试运行该应用程序时遇到了这个顽固的错误,我还没有弄清楚如何进行故障排除。也许这里有人对我犯错误的地方有一些提示。

提供一些背景:

  • 我正在开发一个包含目录子集的项目,每个目录都是一个应用程序(服务器、客户端……)
  • 主目录是git目录
  • 我正在开发的 Nodejs 应用程序是使用 TypeScript 构建的,并利用 Apollo Server(它本身利用了 Express)
  • 我正在通过 GitHub 部署到 Azure

考虑到这一点,我的项目文件夹结构如下所示:

| main_folder
| .github
| workflows
workflow.yml
| server
| src
| dist
.env
index.ts
package-lock.json
package.json
tsconfig.json
| other_apps
.gitignore
README.md

index.ts 中,我有与运行应用程序相关的代码。其余相关文件如下所示:tsconfig.json

{
"compilerOptions": {
"target": "ES2022",
"module": "CommonJS",
"lib": [
"ES2022", "DOM"
],
"strict": true,
"rootDir": ".",
"outDir": "./dist",
"sourceMap": true,
"esModuleInterop": true
}
}

package.json

{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "ts-node-dev --transpile-only --no-notify --exit-child index.ts",
"build": "npx tsc",
"start": "node ."
},
// ...
}

workflow.yml

on:
push:
branches: ["master"]
workflow_dispatch:

env:
AZURE_WEBAPP_NAME: my-app-name
AZURE_WEBAPP_PACKAGE_PATH: "." # set this to the path to your web app project, defaults to the repository root
NODE_VERSION: "16.x" # set this to the node version to use

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./server
steps:
- uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: "npm"
cache-dependency-path: ./server/package-lock.json

- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm run test --if-present

- name: Zip artifact for deployment
run: zip -r release.zip ./* .env

- name: Upload artifact for deployment job
uses: actions/upload-artifact@v3
with:
name: node-app
path: ./server/release.zip

deploy:
permissions:
contents: none
runs-on: ubuntu-latest
needs: build
environment:
name: "Development"
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

steps:
- name: Download artifact from build job
uses: actions/download-artifact@v3
with:
name: node-app

- name: unzip artifact for deployment
run: unzip release.zip

- name: "Deploy to Azure WebApp"
id: my_app_id
uses: azure/webapps-deploy@v2
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}

这个应用程序在本地运行得非常好(运行npm run dev)。当我推送到 GitHub 中的 master 时,构建和部署作业都正常工作。但是,当我转到 Azure 门户并检查该应用程序时,我看到以下两件事:

当我打开应用程序的网址时,页面显示: App failing to access

当我在 Azure 中检查应用程序的诊断时,它告诉我: Azure diagnostics of the app

错误为错误:找不到模块“/home/site/wwwroot/index.js”。在 GitHub 中设置自动构建和部署的配置时,我遇到了项目结构的几个问题,因为我的 git 存储库实际上不包含任何应用程序 (package.json),但它们位于嵌套文件夹中。但是,我认为这个错误不是源于该设置,而是源于其他地方。

有人可以提示我我在这里做错了什么吗?我要重复一遍,这是我第一次尝试将 Nodejs 应用程序发布到 Azure,尽管我认为我正确地遵循了文档,但我有可能在一个明显的步骤中搞砸了。如果是这样的话,我将不胜感激有人指出这一点!

我很乐意在描述中添加可能与该问题相关的更多信息。

最佳答案

我尝试通过创建示例 Node.js Web 应用程序来重现此问题,并将代码推送到 github。

使用 github actions CI/CD 管道创建 Azure 应用服务并成功部署。

确保您具有以下项目结构:

enter image description here

使用门户中的 azure 应用服务的发布配置文件在 GitHub 中创建 key ,如下图所示:

enter image description here

配置完上述步骤后,将代码从本地存储库推送到 git 存储库,然后它将触发 github 管道,代码将部署到 azure 应用服务,如下图所示:

enter image description here

您可以转到azure门户并检查部署是否成功,如下所示:

enter image description here

单击 Azure 应用服务 URL 后的输出屏幕: enter image description here

关于node.js - 如何使用部署到 Azure 的应用程序修复 "Error: Cannot find module '/home/site/wwwroot/index.js”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74005695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com