【发布时间】: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”上,但没有用)
【问题讨论】:
-
看看stackoverflow.com/questions/53553772/…这可能对你有帮助
-
我们现在面临同样的问题。你找到解决方案了吗?
标签: typescript visual-studio-code visual-studio-debugging tsconfig