8

我想在打字稿文件上调试和设置断点,并在使用 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"}
  }      

有任何想法吗?

4

5 回答 5

13

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

我们需要nodemon在更改时重新启动我们的应用程序,我们需要ts-node/register来运行我们的 typescrypt,并且我们需要设置 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 存储库。

于 2018-10-21T00:30:35.300 回答
2

您绝对应该检查ts-node-dev恕我直言,它比nodemon观看编译更快,因为它在重新启动之间共享Typescript 编译过程。下面是示例 vscodelaunch.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

于 2021-04-01T01:50:29.973 回答
1

Without using ts-node, you can restart on change with this config

task.json

This task watch ts files and compiles them on save

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

then in launch.json,

nodemon reload on change (built files are in dist directory in my case)

       {
            "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
        }

于 2019-04-04T15:33:18.383 回答
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"
        }
    ]
}
于 2020-01-23T14:06:39.037 回答
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} 变量包含当前打开的文件)
  • 为 node 传递 --nolazy 参数,它告诉 v8 提前编译您的代码,以便断点正常工作
  • 传入 -r ts-node/register for node,确保在尝试执行代码之前加载 ts-node
  • 将工作目录设置为项目根目录 - ${workspaceRoot}
  • 将节点调试协议设置为 V8 Inspector 模式。

我怀疑你错过了

 "runtimeArgs": ["--nolazy"]

在您启动配置时。

于 2018-01-26T23:43:52.903 回答