调试并重新启动types typescript vscode

时间:2018-01-26 23:11:11

标签: typescript debugging visual-studio-code vscode-tasks

我想在typescript文件上调试和设置断点,并在使用VSCode调试器配置进行更改(如nodemon监视更改)时重新启动调试器。

到目前为止,我通过VSCode实现了运行,并在没有调试的情况下重新启动了更改。

这是我的launch.json:

{
    "name": "Launch Typescript Server Debugger",
    "request": "launch",
    "type": "node",
    "cwd": "${workspaceRoot}",
    "protocol": "inspector",
    "stopOnEntry": false,
    "program": "${workspaceRoot}/node_modules/nodemon/bin/nodemon",
    "args": [
      "--watch",
      "src/**/*.ts",
      "--ignore",
      "src/**/*.spec.ts",
      "--exec",
      "${workspaceRoot}/node_modules/.bin/ts-node",
      "--inspect",
      "src/app.ts"
    ],
    "restart": true,        
    "env": { "NODE_ENV": "dev"}
  }      

有什么想法吗?

5 个答案:

答案 0 :(得分:1)

想知道为什么WTF对这个完全自然的问题有这么多评论。这是我的方法:

我们需要 nodemon 以在更改时重新启动我们的应用,我们需要 ts-node / register 来运行我们的类型加密,并且我们需要设置vscode的启动器脚本来重新连接调试器应用重新编译后。因此,安装nodemon,ts-node并将此脚本添加到package.json:

"watch:debug": "nodemon --inspect=5858 -e ts,tsx --exec node -r ts-node/register ./src/index.ts"

然后在launch.json中添加配置:

{
  "name": "Attach to Process",
  "type": "node",
  "request": "attach",
  "restart": true,
  "port": 5858,
  "outFiles": [],
  "sourceMaps": true
},

仅此而已,现在我可以使用 yarn watch:debug 启动我的应用程序并附加调试器。 如果仍然遇到问题,请查看我的Github存储库here

答案 1 :(得分:1)

您绝对应该检查一下 ts-node-dev 哪个恕我直言在观看编译方面比 nodemon 更快,因为它在重新启动之间共享 Typescript 编译过程。下面是示例 vscode launch.json 配置,可让您设置断点(调试)以及在更改时重新加载。

{
  "version": "1.0.0",
  "configurations": [
    {
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "name": "Local Server",
      "restart": true,
      "request": "launch",
      "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/ts-node-dev",
      "skipFiles": [ "<node_internals>/**" ],
      "type": "node",
      "runtimeArgs": [ "--respawn" ],
      "args": [ "${workspaceFolder}/src/script/local.server.ts" ]
    }
  ]
}

现在您可以按 F5 或使用调试窗格开始调试/实时重新加载。

如果您碰巧使用 aws lambda

开发,我会为此打包一个小型库
https://github.com/vcfvct/ts-lambda-local-dev

答案 2 :(得分:0)

还不清楚你究竟在问什么,但这可能会有所帮助。 尝试在

中添加这些配置
{
 "name": "Current TS File",
 "type": "node",
 "request": "launch",
 "args": ["${relativeFile}"],
 "runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
 "sourceMaps": true,
 "cwd": "${workspaceRoot}",
 "protocol": "inspector",
}

此配置:

  • 设置一个节点任务,在VS Code中启动当前打开的文件($ {relativeFile}变量包含当前打开的文件)
  • 传入--nolazy arg for node,告诉v8提前编译代码,以便断点正常工作
  • 传入-r ts-node / register for node,确保在尝试执行代码之前加载ts-node
  • 将工作目录设置为项目根目录 - $ {workspaceRoot}
  • 将节点调试协议设置为V8 Inspector模式。

我怀疑你是否错过了

 "runtimeArgs": ["--nolazy"]

在启动配置中。

答案 3 :(得分:0)

无需使用ts-node,就可以使用此配置更改后重新启动

task.json

此任务监视ts文件并在保存时对其进行编译

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "typescript",
            "type": "typescript",
            "tsconfig": "tsconfig.json",
            "problemMatcher": ["$tsc-watch"],
            "option": "watch"
        }
    ]
}

然后在launch.json中

nodemon在更改时重新加载(在我的情况下,内置文件位于dist目录中

       {
            "type": "node",
            "request": "launch",
            "runtimeExecutable": "nodemon",
            "args": ["--watch", "dist"],
            "name": "Debug TypeScript in Node.js",
            "preLaunchTask": "typescript",
            "program": "${workspaceFolder}/start.js",
            "cwd": "${workspaceFolder}",
            "protocol": "inspector",
            "outFiles": ["${workspaceFolder}/dist/**/*.js"],
            "restart": true
        }

答案 4 :(得分:0)

这个对我有用。我一起使用Typescript和Node。

这是我的launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/src/index.ts",
            "preLaunchTask": {
                "type": "typescript",
                "tsconfig": "tsconfig.json",
                "option": "watch",
                "problemMatcher": [
                    "$tsc-watch"
                ],
                "group": "build"
            },
            "outFiles": [
                "${workspaceFolder}/lib/**/*.js"
            ],
            "runtimeExecutable": "nodemon",
            "restart": true,
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"
        }
    ]
}