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

如何为Azure Function App配置自定义函数路径(TypeScript)

配置Azure Functions自定义函数根目录解决方案

1. 修改host.json指定函数搜索路径

在项目根目录(c:/dev/aztest)的host.json中添加functionRuntime配置,指定函数的根目录:

{
  "version": "2.0",
  "functionRuntime": {
    "directory": "src/api"
  },
  // 保留原有其他配置项
}

该配置会让Functions运行时自动到src/api目录下搜索所有函数文件夹。

2. 调整TypeScript编译配置(tsconfig.json)

由于原编译输出目录为dist,需修改tsconfig.json确保编译后的文件路径与源码结构对应:

{
  "compilerOptions": {
    "outDir": "./dist/src/api", // 编译后输出到对应路径
    "rootDir": "./src/api", // 指定源码根目录
    // 保留原有其他编译配置项
  },
  "include": [
    "src/api/**/*.ts" // 仅编译src/api下的TypeScript文件
  ],
  "exclude": [
    "node_modules",
    "dist"
  ]
}

修改后执行npm run build,编译后的函数文件会生成在dist/src/api/{函数文件夹名}路径下,与源码结构匹配。

3. 修正函数文件夹内的function.json路径

每个函数文件夹(如src/api/MyTestFunction)中的function.json,需确保scriptFile指向编译后的文件相对路径:

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": ["get", "post"]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ],
  "scriptFile": "../dist/src/api/MyTestFunction/index.js"
}

路径需相对于当前function.json所在位置,确保指向正确的编译后文件。

4. 验证启动命令

回到项目根目录(c:/dev/aztest)执行func start,运行时会读取host.json的配置,自动加载src/api下的函数及对应编译文件。

若仍出现问题,可检查:

  • src/api下的函数文件夹结构是否规范,每个函数需包含function.json和对应的TypeScript文件
  • 重新执行npm run build,确认dist目录下生成了对应结构的编译文件
  • 检查host.json的JSON格式是否正确,无语法错误

内容的提问来源于stack exchange,提问作者MjKFi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 11:39:53