Azure Functions(TypeScript)をデバッグする

2022年7月21日

今回の内容に関するリポジトリは以下です。
テンプレート化しています。
こちらを作成するまでの手順を今回は説明するだけです。

https://github.com/xiaotiantakumi/az-func-ts-starter

前回の内容

やったこと

ホットリロード機能とかブレークポイントでのデバッグをしたいと思ったので調査。
こちらの内容を参考に進める。https://github.com/Azure/azure-functions-core-tools/issues/1239
concurrentlyとnodemonをインストール

odatakuminoMacBook-Air@takumi ~/Documents/Azure/work/ts/functions/sample001$ npm install --save-dev concurrently

added 28 packages, and audited 32 packages in 3s

5 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
odatakuminoMacBook-Air@takumi ~/Documents/Azure/work/ts/functions/sample001$ npm install --save-dev nodemon     

added 33 packages, and audited 65 packages in 846ms

8 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

以下部分を追加
もともとstartというのがありましたが、上書きしてしまってOKです。

    "start": "concurrently npm:start:*",
    "start:tsc": "tsc -w --preserveWatchOutput",
    "start:func": "nodemon --watch dist --delay 1 --exec \"func start -p 7072\""

visual studio codeでのデバッグ用にlaunch.jsonを作成します。

どうせ修正するのでなんでもいいですが、Node.jsを選びました。
できたテンプレートに以下コピペします。

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Azure Function",
      "request": "launch",
      "runtimeArgs": ["run-script", "start"],
      "runtimeVersion": "16",
      "runtimeExecutable": "npm",
      "skipFiles": ["<node_internals>/**"],
      "type": "pwa-node",
      "console": "integratedTerminal"
    },
    {
      "name": "Attach to Node Functions",
      "type": "node",
      "request": "attach",
      "port": 9229,
      "preLaunchTask": "func: host start"
    }
  ]
}

ブレークポイント置いて以下のURLにアクセスしてみる。
http://localhost:7072/api/SampleTrigger
ちゃんと止まりました。