调试并重新启动types typescript vscode

问题描述 投票:1回答:3

我想在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"}
  }      

有任何想法吗?

typescript debugging visual-studio-code vscode-tasks
3个回答
1
投票

想知道为什么这么多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
},

这就是全部,现在我可以用纱线手表启动我的应用程序:调试并附加调试器。如果您仍然遇到问题,请查看我的Github repo here


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节点
  • 将工作目录设置为项目根目录 - $ {workspaceRoot}
  • 将节点调试协议设置为V8 Inspector模式。

我怀疑你错过了

 "runtimeArgs": ["--nolazy"]

在您启动配置。


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
        }

© www.soinside.com 2019 - 2024. All rights reserved.