使用Typescript + VSCode调试Node.js Async / Await [英] Debug Node.js Async/Await with Typescript+VSCode

查看:299
本文介绍了使用Typescript + VSCode调试Node.js Async / Await的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了以下答案:

async await with nodejs 7

如何在visual studio代码中调试async / await?

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

However neither have solved my issue.

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

I want to be able to debug native Async/Await from VSCode using Node.js v7.4.0 without the horrible Typescript transpiled version. I'm able to get Typescript to output the correct code ie no __awaiter etc. However, once I attempt to debug the code, all the transpiled state machine code appears!? So I can debug the code, its just not the code I want to debug. Is there anyway to prevent the debugged code from having the transpiled state machine code?

以下是我的配置文件:

tsconfig.json

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

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代码片段:

To further illustrate what I'm on about, here is a snippet of code in the outputted javascript:

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

然而,在调试时它看起来像这样:

However when debugged it looks like this:

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


推荐答案

最后想出来了。使用Typescript和Jest。分享我所拥有的,希望它会帮助某人。节点11,VScode 1.34.0

Finally figured it out. Using Typescript and Jest. Sharing what I hav, hopefully it will help someone. Node 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是非常令人沮丧的。它不会在未来很多年里继续存在。

However this works only in some scenarios. If you can compile you app to JS ES2017 that works. angular can't be compiled to that es version so. It works only with some test files. It is very frustrating that angular compile does not output es2017. and it wont for many years to come still.

这篇关于使用Typescript + VSCode调试Node.js Async / Await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆