LoginSignup
2
1

More than 1 year has passed since last update.

Visual Studio Codeとts-nodeでシンプルなTypeScriptデバッグ環境を作る

Last updated at Posted at 2021-07-24

はじめに

VSCodeでTypeScriptをデバッグしたい時、割と環境構築が面倒だったりしませんか?ts-nodeを使えば簡単にデバッグできるようになるので、今回はそのやり方をご紹介します。

手順

※プロジェクトディレクトリは作成済みとします
npmを初期化

npm init

TypeScriptをインストール

npm install typescript @types/node@14

※「14」の部分はお使いのNode.jsのバージョンに合わせてください

ts-nodeをインストール

npm install ts-node

launch.jsonにエントリポイントを追加

launch.json
"args": [
    "${workspaceFolder}/src/index.ts"
]

launch.jsonに下記設定を追加

launch.json
"runtimeArgs": [
    "-r",
    "ts-node/register"
]

launch.json全体ではこんな感じです

launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "pwa-node",
            "request": "launch",
            "name": "Launch Program",
            "runtimeArgs": [
                "-r",
                "ts-node/register"
            ],
            "skipFiles": [
                "<node_internals>/**"
            ],
            "args": [
                "${workspaceFolder}/src/index.ts"
            ]
        }
    ]
}

以上で設定は完了です。シンプルでしょ?

動作確認

プロジェクトディレクトリ直下にsrcディレクトリを作成し、下記のファイルを配置します

index.ts
import {testClass} from "./test";

const test = new testClass();
test.testFunction();
test.ts
export class testClass {
    testFunction() {
        console.log("testFunction has called");
    }
}

エントリポイントであるindex.tstestFunctionを呼び出すだけですね

最後に、好きな箇所にブレークポイントをセットしてデバッグ画面で先程作成したlaunch.jsonを選択し、実行ボタンをクリックします
スクリーンショット 2021-07-24 22.48.37.png

まずtestFunctionの呼び出し元がヒットし
スクリーンショット 2021-07-24 22.52.15.png

次にtestFunctionの内部がヒットすることを確認できました
スクリーンショット 2021-07-24 22.52.28.png

今回は以上です。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1