Reputation: 321
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)
Upvotes: 11
Views: 3110
Reputation: 57482
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/"
}
}
]
}
Upvotes: 1