Home > Development Tools > VSCode > body text

How to debug ts in vscode

藏色散人
Release: 2020-04-08 10:23:13
Original
6809 people have browsed it

How to debug ts in vscode

How to debug ts with vscode?

vscode Debug TypeScript

Environment

typescript:2.5.2

vscode:1.16.0

vscode directly debug ts files

Source code:github

(https://github.com/meteor199/my-demo/tree/master/typescript/vscode-debug )

How to debug ts in vscode

Install typescript dependencies

npm install typescript --save-dev
Copy after login

Add tsconfig.json

The main thing is to set sourceMap to true.

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": true,
        "outDir": "./dist",
        "sourceMap": true
    },
    "include": [
        "src/**/*"
    ]
}
Copy after login
Copy after login

Configure automatic compilation

Use vscode's tasks to automatically compile ts into js. You can also use other methods to compile, such as: gulp, webpack, etc.

Add file: /.vscode/tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for thedocumentation about the tasks.json format
   "version": "0.1.0",
   "command": "tsc",
   "isShellCommand": true,
   //-p 指定目录;-w watch,检测文件改变自动编译
   "args": ["-p", ".","-w"],
   "showOutput": "always",
   "problemMatcher": "$tsc"
}
Copy after login

Use the shortcut key Ctrl Shift B to turn on automatic compilation.

Configuring debugging

When debugging, you need to configure the launch.json file of vscode. This file records startup options.

Add or edit the file /.vscode/launch.json.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "launch",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/dist/main.js",
            "args": [],
            "cwd": "${workspaceRoot}",
            "protocol": "inspector"
        }
    ]
}
Copy after login

Note: program needs to be set to the corresponding js generated by the ts you want to debug.

If you need to debug /src/main.ts, this is ${workspaceRoot}/dist/main.js.

Debugging

Open main.ts, add a breakpoint on the left side for debugging.

How to debug ts in vscode

Use ts-node to debug ts files

Source code: github (https://github.com/meteor199/my-demo/tree/master/ typescript/vscode-debug-without-compiling)

From: Debugging TypeScript in VS Code without compiling, using ts-node

ts-node When debugging ts files, js will not be explicitly generated . If you don't want to compile it into js and then debug it, you can consider this method.

Install npm dependency package

npm install typescript --save-dev
npm install ts-node --save-dev
Copy after login

Configure tsconfig.json

The main thing is to set sourceMap to true.

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": true,
        "outDir": "./dist",
        "sourceMap": true
    },
    "include": [
        "src/**/*"
    ]
}
Copy after login
Copy after login

Configuration launch.json

Open the DEBUG interface, add configuration

or edit /.vscode/launch.json.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Current TS File",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/node_modules/ts-node/dist/_bin.js",
            "args": [
                "${relativeFile}"
            ],
            "cwd": "${workspaceRoot}",
            "protocol": "inspector"
        }
    ]
}
Copy after login

Debugging

Open the ts file to be debugged and add the debugger.

Open the debug interface.

After DEBUG, select the corresponding configuration in launch.json, here is Current TS File.

Click the run button or press F5 to run.

How to debug ts in vscode

Related tutorial recommendations: vscode tutorial

The above is the detailed content of How to debug ts in vscode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!