You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署Node.js应用到Azure时遇“找不到index.js”错误的排查求助

问题:Azure App Service部署Node.js(TypeScript)应用后提示Error: Cannot find module '/home/site/wwwroot/index.js'

背景信息

  • 项目为多子目录结构,主目录是Git仓库,目标应用位于server子目录
  • 应用基于TypeScript开发,使用Apollo Server + Express
  • 通过GitHub Actions部署到Azure App Service
  • 本地运行npm run dev正常,GitHub构建、部署任务均显示成功,但Azure应用无法访问,诊断提示找不到/home/site/wwwroot/index.js

项目结构:

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

关键配置文件:

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(核心片段)

env:
  AZURE_WEBAPP_PACKAGE_PATH: "." 

jobs:
  build:
    defaults:
      run:
        working-directory: ./server
    steps:
      # ... 其他步骤
      - name: npm install, build, and test
        run: |
          npm install
          npm run build --if-present
      - name: Zip artifact for deployment
        run: zip -r release.zip ./* .env

  deploy:
    steps:
      # ... 下载解压artifact
      - name: "Deploy to Azure WebApp"
        uses: azure/webapps-deploy@v2
        with:
          package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}

错误原因分析

  1. 编译后入口文件路径不匹配
    根据tsconfig.json配置,outDir设为./dist,TypeScript编译后的所有产物(包括根目录的index.ts)都会生成到dist目录下,即dist/index.js是实际可执行的入口文件。但package.json中:

    • main字段指向的是根目录的index.js(不存在,根目录只有index.ts)
    • start脚本使用node .,会默认读取main字段指向的文件,自然找不到不存在的index.js
  2. 部署路径验证
    GitHub Actions的打包步骤已经将dist目录包含在压缩包中,部署后/home/site/wwwroot下会存在dist目录,但启动命令没有指向该目录下的入口文件。

解决方案

  1. 修正package.json配置
    修改main字段和start脚本,指向编译后的正确路径:

    {
      "main": "dist/index.js",
      "scripts": {
        "start": "node dist/index.js"
      }
    }
    
  2. 确认Azure启动命令
    在Azure门户的应用服务配置页面,检查"启动命令"是否设置为npm run start(如果依赖package.json脚本),或者直接设置为node dist/index.js,确保启动时指向正确的文件。

  3. 验证构建产物
    在GitHub Actions的build步骤中,可以添加一个验证命令,确认dist目录存在且包含index.js:

    ls -la dist/
    

内容的提问来源于stack exchange,提问作者P.Gracia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 02:40:36