Open2

VSCode で ts-node のデバッグをする方法

たたたたたた

参考:
https://gist.github.com/cecilemuller/2963155d0f249c1544289b78a1cdd695

環境

  • ts-nodeでESMを利用

コード

launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Current TS File",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "node",
      "runtimeArgs": ["--loader", "ts-node/esm"],
      "args": ["${file}", "--example", "hello"],
      "cwd": "${workspaceRoot}",
      "internalConsoleOptions": "openOnSessionStart",
      "skipFiles": ["<node_internals>/**", "node_modules/**"]
    }
  ]
}

ただし、「--loader ts-node_esm」はExperimentalWarningが出る。

Node.js debugging in VS Code ドキュメント
https://code.visualstudio.com/docs/nodejs/nodejs-debugging

たたたたたた

エラー解決方法

TSError: ⨯ Unable to compile TypeScript:
error TS5109: Option 'moduleResolution' must be set to 'NodeNext' (or left unspecified) when option 'module' is set to 'NodeNext'.

このエラーが出た場合は、moduleResolution を設定していることにより上手くいかないようです。
現在の記述を削除するか、"moduleResolution": "NodeNext" と指定するとうまくいくようです。
docs: https://www.typescriptlang.org/tsconfig#moduleResolution