如何在vscode错误中调试打字稿节点应用程序

时间:2019-09-16 18:52:45

标签: typescript visual-studio-code

在我的vscode应用程序中,我有一个名为apiEngine /

的文件夹

在此文件夹中是我的节点应用程序。

为什么不在vscode中调试?

enter image description here

enter image description here

.vscode \ launch.json


{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Build Project",
            "program": "${workspaceFolder}\\apiEngine\\src\\index.ts",
            "preLaunchTask": "npm: build",
            "sourceMaps": true,
            "smartStep": true,
            "internalConsoleOptions": "openOnSessionStart",
            "outFiles": [
                "${workspaceFolder}/apiEngine/dist/**/*.js"
            ]
        }
    ]
}

.vscode \ tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "npm",
            "script": "build",
            "path": "apiEngine/",
            "group": "build",
            "problemMatcher": []
        },
        {
            "type": "typescript",
            "tsconfig": "apiEngine/tsconfig.json",
            "problemMatcher": [
                "$tsc"
            ],
            "group": "build"
        }
    ]
}

1 个答案:

答案 0 :(得分:1)

如果设置"preLaunchTask": "npm: build",则VS代码会在工作区根目录(包含build文件夹的目录package.json中使用npm脚本.vscode。它不想在.vscode\tasks.json中执行构建任务。

这也是导致错误的原因-package.json位于apiEngine文件夹中更深一层,因此VS Code无法在工作区根目录中找到脚本。

解决方案

要改为调用IDE任务,可以为构建任务指定label并在npm: build中设置此标签而不是preLaunchTask。任务的path参数将提供正确的package.json位置。

.vscode \ tasks.json:

tasks: [
  {
    label: "npm-build",
    type: "npm",
    script: "build",
    path: "apiEngine/",
    group: "build",
    problemMatcher: []
  },
  ...
];

.vscode \ launch.json:

...
// before: 
// "preLaunchTask": "npm: build"
// after:
"preLaunchTask": "npm-build", 
...
相关问题