【问题标题】:Vscode debugger cannot resolve tsconfig.json pathvscode 调试器无法解析 tsconfig.json 路径
【发布时间】:2019-06-22 11:34:06
【问题描述】:

我目前正在使用 typescript 使用 node-express,我正在通过 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”上,但没有用)

【问题讨论】:

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


【解决方案1】:

要让它工作,你需要通过设置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/"
      }
    }
  ]
}

【讨论】:

    猜你喜欢
    • 2021-02-20
    • 2019-08-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 2017-08-18
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多