TypeScriptで書いたFirebase FunctionsのコードをVSCodeデバッガから起動する

#typescript
#vscode

VSCode から TypeScript のコードを tsc でコンパイルせず直接実行したい場合は、node の実行時引数から ts-node を使うように launch.json の設定を書けばいい。

https://github.com/TypeStrong/ts-node#visual-studio-code

{ "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Launch Program", "runtimeArgs": ["-r", "ts-node/register"], "args": ["${file}"] } ] }

Firebase Functions を扱うリポジトリでは、package.json が functions/ ディレクトリ下に入っていて、リポジトリルートには無い。この functions/package.json に ts-node の依存が定義されている場合は、以下のように node_modules の場所を VSCode に知らせればデバッガで ts-node を使うことができる。

{ "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Launch Program", "runtimeArgs": ["-r", "ts-node/register"], "args": ["${file}"], "env": { "NODE_PATH": "./functions/node_modules" } } ] }

node.js - Specify path to node_modules in package.json - Stack Overflow

Tweet