Vscode debugger cannot resolve tsconfig.json path
Asked Answered
T

1

11

I'm currently working node-express with typescript, I'm using absolute import path through tsconfig.json file's baseUrl and paths, and run server with ts-node and using tsconfig-paths module to ts-node can recognize my tsconfig absolute path on run time. I have no problem with running server through npm start,

But whenever I started server with debugging mode on vscode, It seems cannot resolve my tsconfig's path and throwing error cannot find module 'my absolute module path'. here is my tsconfig.json file and package.json file

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

"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\""
  },

note that I attach "tsconfig-paths/register" on npm start script. and here is my debugging mode setting in 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"
    }

How can I set dubugging mode to resolve my tsconfig.json's path. and is there any way to use absolute path with vscode debugger? (I've tried put "tsconfig-paths/register" on "runtimeArgs" but did not work)

Turbot answered 29/1, 2019 at 10:51 Comment(2)
take a look #53554272 this might help youSymptomatic
We are facing the same issue right now. Did you ever find a solution?Hypothecate
W
1

To get this to work, you need to tell VS Code where the root path is by setting the NODE_PATH environment variable. Since you're outputing the compiled JavaScript code into the dist folder, set the env NODE_PATH variable as follows in launch.json:

{
  "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/"
      }
    }
  ]
}
Westbrooke answered 28/1, 2021 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.