【问题标题】:Incorrect breakpoint location using VS Code, TypeScript and Node.js使用 VS Code、TypeScript 和 Node.js 的断点位置不正确
【发布时间】:2016-01-30 16:25:37
【问题描述】:

尝试在 VS Code 中使用 TypeScript 调试节点应用程序。

问题是调试器没有停在源代码中的正确位置。

app.ts

class Foo 
{
    doSomething(){
        console.log("TEST");
    }   
}

var foo = new Foo()
foo.doSomething();

tsconfig.json

{
    "compilerOptions": {
        "sourceMap": true,
        "target": "ES5",
        "module": "commonjs"
    }
}

launch.json

"configurations": [
    {
        // Name of configuration; appears in the launch configuration drop down menu.
        "name": "Launch app.js",
        // Type of configuration.
        "type": "node",
        // Workspace relative or absolute path to the program.
        "program": "app.ts",
        // Automatically stop program after launch.
        "stopOnEntry": false,
        // Command line arguments passed to the program.
        "args": [],
        // Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
        "cwd": ".",
        // Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
        "runtimeExecutable": null,
        // Optional arguments passed to the runtime executable.
        "runtimeArgs": ["--nolazy"],
        // Environment variables passed to the program.
        "env": {
            "NODE_ENV": "development"
        },
        // Use JavaScript source maps (if they exist).
        "sourceMaps": true,
        // If JavaScript source maps are enabled, the generated code is expected in this directory.
        "outDir": null
    },

tasks.json

// A task runner that calls the Typescript compiler (tsc) and
// compiles based on a tsconfig.json file that is present in
// the root of the folder open in VSCode

{
    "version": "0.1.0",

    // The command is tsc. Assumes that tsc has been installed using npm install -g typescript
    "command": "tsc",

    // The command is a shell script
    "isShellCommand": true,

    // Show the output window only if unrecognized errors occur.
    "showOutput": "silent",

    // Tell the tsc compiler to use the tsconfig.json from the open folder.
    "args": ["-p", "."],

    // use the standard tsc problem matcher to find compile problems
    // in the output.
    "problemMatcher": "$tsc"
}

问题是如果我在"console.log("test");"处设置断点行,调试器跳下两行并错过了我的断点。

知道为什么会这样。据我了解,应该可以调试用打字稿编写的节点应用程序。

【问题讨论】:

标签: javascript node.js typescript visual-studio-code


【解决方案1】:

我怀疑原因是您的 app.jsapp.map.js 在某些时候更改时没有更新。

您应该在每次运行调试器之前运行 tsc 任务,以确保生成的 JavaScript 文件反映您在 TypeScript 文件中所做的更改。

可以在https://github.com/jyuhuan/node-ts 找到这个(非常常见的)用例的简单项目模板。它定义了一个 tsc 任务来监控 TS 文件的任何变化,使生成的 JS 文件始终保持最新。

【讨论】:

    猜你喜欢
    • 2018-08-11
    • 2016-04-15
    • 2018-10-16
    • 2017-06-24
    • 2021-10-05
    • 1970-01-01
    • 2018-10-30
    • 2015-12-20
    • 2018-11-21
    相关资源
    最近更新 更多