首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Typescript+VSCode调试Node.js异步/等待

使用Typescript+VSCode调试Node.js异步/等待
EN

Stack Overflow用户
提问于 2017-01-14 07:06:08
回答 1查看 6K关注 0票数 8

我已经检查了以下答案:

async await with nodejs 7

How to debug async/await in visual studio code?

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

我希望能够使用Node.js v7.4.0从VSCode调试本机异步/等待,而不是可怕的Typescript转译版本。我能够让Typescript输出正确的代码,即没有__awaiter等。但是,一旦我尝试调试代码,所有转换的状态机代码都会出现!?所以我可以调试代码,这不是我想要调试的代码。有没有什么方法可以防止被调试的代码拥有被转译的状态机代码?

以下是我拥有的配置文件:

tsconfig.json

代码语言:javascript
复制
{
    "compilerOptions": {
        "target": "es2017",

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

launch.json

代码语言:javascript
复制
{
    "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中的一段代码:

代码语言:javascript
复制
let handler = subscription.messageSubscription.handler;
debugger;
await handler(message.message, context);

但是,在调试时,它看起来如下所示:

代码语言:javascript
复制
case 4:
    handler = subscription.messageSubscription.handler;
    debugger;
    return [4 /*yield*/, handler(message.message, context)];
case 5:
EN

回答 1

Stack Overflow用户

发布于 2019-05-25 16:23:33

终于想明白了。使用Typescript和Jest。分享我所拥有的,希望能帮助到一些人。节点11,VScode 1.34.0

代码语言:javascript
复制
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版本。它只适用于某些测试文件。非常令人沮丧的是,angular编译器不能输出es2017。而且它不会在很多年后还会出现。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41644827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档