typescript 无法在具有typscript项目的Express js中使用vscode调试器

pnwntuvh  于 2022-12-05  发布在  TypeScript
关注(0)|答案(1)|浏览(94)

下面是我的package.json文件
`

{
  "name": "crm-backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "concurrently \"yarn tsc --watch\" \"yarn tsc-alias --watch\" \"nodemon -q dist/index.js\""
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "mongoose": "^6.7.5"
  },
  "devDependencies": {
    "@types/express": "^4.17.14",
    "@types/mongoose": "^5.11.97",
    "@types/node": "^18.11.10",
    "concurrently": "^7.6.0",
    "nodemon": "^2.0.20",
    "tsc-alias": "^1.8.1",
    "typescript": "^4.9.3"
  }
}

我在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": [
    {
      "name": "Attach",
      "port": 1234,
      "request": "attach",
      "skipFiles": [
        "<node_internals>/**"
      ],
      "type": "node"
    },
    {
      "name": "Launch Chrome with Debugger",
      "port": 7999,
      "request": "launch",
      "type": "chrome",
      "webRoot": "${workspaceFolder}/static",
    },
    {
      "name": "Nodemon: Attach Express.js + TypeScript 2",
      "type": "node",
      "request": "attach",
      "port": 9229,
      "restart": true,
      "protocol": "inspector",
      "cwd": "${workspaceFolder}/backend"
    }
  ]
}

`
只有“无恶魔:Attach Express.js + TypeScript”工作正常(为此我必须在package.json中nodemon writer后面添加“--inspect-brk=0.0.0.0:9229”),但它捕获了输出编译的“js”文件的断点,而不是源“ts”文件。(我必须添加。

pcrecxhr

pcrecxhr1#

只需将以下内容添加到您的tsconfig文件中:

"compilerOptions": {
    "sourceMap": true
}

它会将编译后的javascriptMap回typescript以进行调试,但是在构建产品包时不要忘记关闭它(可能有tsconfig.prod文件),因为源代码Map会增加包的大小

相关问题