使用Typescript + VSCode调试Node.js Async / Await

时间:2017-01-13 23:06:08

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

我检查了以下答案:

async await with nodejs 7

How to debug async/await in visual studio code?

然而,两者都没有解决我的问题。

我希望能够使用Node.js v7.4.0从VSCode调试本机Async / Await,而不会使用可怕的Typescript转换版本。我能够得到Typescript输出正确的代码,即没有__awaiter等。但是,一旦我尝试调试代码,所有转换后的状态机代码出现了!?所以我可以调试代码,它只是我想要调试的代码。反正是否有阻止调试代码具有转换后的状态机代码?

以下是我的配置文件:

tsconfig.json

{
    "compilerOptions": {
        "target": "es2017",

        "module": "commonjs",
        "noImplicitAny": false,
        "sourceMap": true,
        "outDir": "lib",
        "noUnusedParameters": false,
        "noUnusedLocals": false,
        "skipLibCheck": true
        //"importHelpers": true
    },
        "exclude": [
        "node_modules"
    ]
}

launch.json

{
    "name": "Launch",
    "type": "node",
    "request": "launch",
    "program": "${workspaceRoot}/node_modules/jest-cli/bin/jest.js",
    "stopOnEntry": false,
    "cwd": "${workspaceRoot}",
    //"preLaunchTask": "tsc",
    "runtimeExecutable": null,
    "args": [
        "--runInBand"
    ],
    "runtimeArgs": [
        "--harmony-async-await",
        "--no-deprecation"
    ],
    "env": {
        "NODE_ENV": "development"
    },
    "console": "integratedTerminal",
    "sourceMaps": true,
    "outFiles": [
        "${workspaceRoot}/{lib}/**/*.js"
    ],
    "skipFiles": [
        "node_modules/**/*.js",
        "lib/**/*.js"
    ]
}

为了进一步说明我的目标,这里输出的javascript中有一段代码:

let handler = subscription.messageSubscription.handler;
debugger;
await handler(message.message, context);

但是在调试时它看起来像这样:

case 4:
    handler = subscription.messageSubscription.handler;
    debugger;
    return [4 /*yield*/, handler(message.message, context)];
case 5:

2 个答案:

答案 0 :(得分:0)

我将"smartStep": true添加到 launch.json ,并根据需要调试await / async模式(使用Node v8.4.0)。

这是我的launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${workspaceRoot}/src/main.ts",
      "cwd": "${workspaceRoot}",
      "console": "integratedTerminal",
      "outFiles": [ "${workspaceRoot}/dist/*.js" ],
      "sourceMaps": true,
      "preLaunchTask": "build",
      "smartStep": true
    }
  ]

}

有关详细信息,请参阅https://code.visualstudio.com/updates/vApril#_smart-code-stepping

这不是一个完美的解决方案,因为使用 smartStep 您无法调试到库代码中,因此如果要调试到库中,则可以手动注释掉此选项。也许有人知道如何解决这个小小的不便。

答案 1 :(得分:0)

最后弄清楚了。使用打字稿和玩笑。分享我所拥有的,希望对别人有所帮助。节点11,VScode 1.34.0

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Jest Current File",
            "program": "${workspaceFolder}/node_modules/.bin/jest",
            "args": ["-i", "${relativeFile}"],                
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "disableOptimisticBPs": true,
            "sourceMaps": true,
            "smartStep": true,
            "windows": {
                "program": "${workspaceFolder}/node_modules/jest/bin/jest"
            }
        }
    ]
}


tsconfig.json:

{
    "compileOnSave": false,
    "compilerOptions": {
        "baseUrl": ".",
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "inlineSources": true,
        "sourceRoot": "/",
        "declaration": false,
        "module": "es2015",
        "esModuleInterop": true,
        "resolveJsonModule": true,
        "stripInternal": true,
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es2017",
        "typeRoots": ["node_modules/@types"],
        "lib": ["es2018", "dom", "esnext.asynciterable"],
        "types": ["chrome", "node", "jest"]         
    }
}

但是,这仅在某些情况下有效。如果可以将您的应用程序编译为可以运行的JS ES2017。无法将angular编译为该es版本。它仅适用于某些测试文件。令人沮丧的是,角度编译不输出es2017。而且它不会持续很多年。