Vscode调试器无法解析tsconfig.json路径

时间:2019-01-29 10:51:00

标签: typescript visual-studio-code visual-studio-debugging tsconfig

我目前正在使用nodescript进行节点表达,我正在通过tsconfig.json文件的baseUrl和路径使用绝对导入路径,并在ts-node上运行服务器,并使用tsconfig-paths模块来对ts-node进行识别tsconfig运行时的绝对路径。通过 npm start

运行服务器没有问题

但是,每当我在vscode上以调试模式启动服务器时,似乎都无法解析tsconfig的路径,并且抛出错误无法找到模块“我的绝对模块路径”。这是我的tsconfig.json文件和package.json文件

tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist",
    "moduleResolution": "node",
    "sourceMap": true,
    "module": "commonjs",
    "allowJs": true,
    "target": "es5",
    "noUnusedParameters": false,
    "allowUnreachableCode": true,
    "allowUnusedLabels": true,
    "lib": [
      "es2015"
    ],
    "baseUrl": ".",
    "paths": {
      "@routes": [
        "src/routes/*"
      ],
      "@client": [
        "../client/*"
      ]
    },
  }
}

package.json-脚本

"scripts": {
    "test": "jest --watchAll --runInBand",
    "coverage": "jest --coverage",
    "start": "ts-node -r tsconfig-paths/register src/index.ts",
    "server": "./node_modules/nodemon/bin/nodemon.js",
    "client": "npm start --prefix ../client",
    "dev": "concurrently \"npm run server\" \"npm run client\""
  },

请注意,我在npm启动脚本上附加了“ tsconfig-paths / register”。这是我在launch.json中的调试模式设置 launch.json

{
      "name": "TS File",
      "type": "node",
      "request": "launch",
      "args": [
        "${workspaceRoot}/server/src/index.ts"
      ],
      "runtimeArgs": [
        "--nolazy",
        "-r",
        "ts-node/register"
      ],
      "cwd": "${workspaceRoot}/server",
      "protocol": "inspector",
      "internalConsoleOptions": "openOnSessionStart",
      "console": "integratedTerminal"
    }

如何设置调试模式来解析tsconfig.json的路径。并且有什么方法可以在vscode调试器中使用绝对路径? (我尝试将“ tsconfig-paths / register”放在“ runtimeArgs”上,但没有用)

1 个答案:

答案 0 :(得分:0)

要使其工作,您需要通过设置 NODE_PATH 环境变量来告诉 VS Code 根路径在哪里。由于您将编译后的 JavaScript 代码输出到 dist 文件夹中,因此请在 launch.json 中按如下方式设置 env NODE_PATH 变量:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": ["<node_internals>/**"],
      "program": "${workspaceFolder}/src/index.ts",
      "preLaunchTask": "tsc: build - tsconfig.json",
      "outFiles": ["${workspaceFolder}/dist/**/*.js"],
      "env": {
        "NODE_PATH": "dist/"
      }
    }
  ]
}