Typescript 使用vscode调试nest.js应用程序
我正在测试nest.js框架,但我正在努力使用VSCode运行它,以便能够正确地调试代码。这与这里描述的问题几乎相同。然而,我确保我使用的是最新的软件包。我总是会遇到这样的错误:Typescript 使用vscode调试nest.js应用程序,typescript,Typescript,我正在测试nest.js框架,但我正在努力使用VSCode运行它,以便能够正确地调试代码。这与这里描述的问题几乎相同。然而,我确保我使用的是最新的软件包。我总是会遇到这样的错误: Error: Cannot find module 'cats/cats.module' at Function.Module._resolveFilename (module.js:485:15) at Function.Module._load (module.js:437:25) at M
Error: Cannot find module 'cats/cats.module'
at Function.Module._resolveFilename (module.js:485:15)
at Function.Module._load (module.js:437:25)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (c:\Users\user\Documents\random-api\dist\app.module.js:11:26)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
我的vscode的启动。json:
"dependencies": {
"@nestjs/common": "^4.6.6",
"@nestjs/core": "^4.6.6",
"@nestjs/microservices": "^4.6.6",
"@nestjs/testing": "^4.6.6",
"@nestjs/websockets": "^4.6.6",
"reflect-metadata": "^0.1.12",
"rxjs": "^5.5.7",
"typescript": "^2.7.2"
},
"devDependencies": {
"@types/express": "^4.11.1",
"@types/jest": "^22.2.2",
"@types/node": "^9.6.0",
"@types/supertest": "^2.0.4",
"jest": "^22.4.3",
"nodemon": "^1.17.2",
"prettier": "^1.11.1",
"supertest": "^3.0.0",
"ts-jest": "^22.4.2",
"ts-node": "^5.0.1",
"tsconfig-paths": "^3.1.3",
"tslint": "5.9.1",
"tslint-microsoft-contrib": "^5.0.3"
},
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\dist\\main.js",
"smartStep": true,
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}
我使用typescript文件作为路径尝试了相同的launch.json,但引发了相同的异常:
"program": "${workspaceFolder}\\src\\main.ts",
试试这个launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Nest Framework",
"args": ["${workspaceFolder}/src/main.ts"],
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector"
}
]
}
{
"type": "node",
"request": "launch",
"name": "Nest Debug",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"start:debug",
"--",
"--inspect-brk"
],
"console": "integratedTerminal",
"restart": true,
"protocol": "auto",
"port": 9229,
"autoAttachChildProcesses": true
},
在launch.json中
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Nest Framework",
"args": ["${workspaceFolder}/src/main.ts"],
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector"
}
]
}
下面是一个有效的配置。希望这对某人有帮助:) 确保在
runtimeArgs
下添加tsconfig path/register
行,否则会出现一个错误,说明未找到某些用户定义的模块
如果根项目文件夹下有应用程序文件夹名称,请将其替换为应用程序文件夹名称,否则将其从脚本中的路径中删除
注意:在vscode上执行此调试配置之前,请确保停止运行应用程序,因为此调试脚本将在同一端口上启动应用程序的新实例
{
“版本”:“0.2.0”,
“配置”:[
{
“类型”:“节点”,
“请求”:“启动”,
“名称”:“调试嵌套框架”,
“args”:[
“${workspaceFolder}//src/main.ts”
],
“runtimeArgs”:[
“--nolazy”,
“-r”,
“ts节点/寄存器”,
“-r”,
“tsconfig路径/寄存器”
],
“源地图”:正确,
“cwd”:“${workspaceRoot}/”,
“协议”:“检查员”
}
]
}
此设置适用于我
{
"name": "Launch app",
"type": "node",
"request": "launch",
"args": [
"src/main.ts"
],
"runtimeArgs": [
"-r",
"ts-node/register",
"-r",
"tsconfig-paths/register"
],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart",
"env": {
"NODE_ENV": "local",
"NODE_PORT": "9000"
},
"sourceMaps": true,
"console": "internalConsole",
"outputCapture": "std"
}
从获取它我尝试了接受的答案和所有其他差异,但没有一个对我有效,但是真正对我有效的是连接到9229端口。我所做的是使用以下配置添加/修改您的
launch.json
.vscode/launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach NestJS WS",
"port": 9229,
"restart": true,
"stopOnEntry": false,
"protocol": "inspector"
}
]
}
在package.json
中(嵌套新的CLI命令,需要6.8.x
,请参见此)
终于成功了 我所做的是使用package.json中的一个脚本自动附加vs代码调试过程。除此之外,我还使用了nodemon,如果您在开发过程中做了任何更改,它将与调试器一起自动重新启动 该过程要求您全局安装nodemon,并在文件夹的根目录中添加一个nodemon-debug.json文件,如下所示 nodemon debug.json
{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register src/main.ts"
}
然后在package.json中,添加如下所示的脚本
"debug": "nodemon --config nodemon-debug.json"
然后在VS代码中,点击F1搜索调试:切换自动附加。点击它来启用它
然后运行以下命令启动调试过程-
npm run debug
调试器将自动打开
此过程的优点是nodemon,每当您对代码进行一些更改并需要它时,它会自动与调试器一起启动
要获得更详细的解释,请浏览以下内容。为我工作。我只需通过@JWess“升级”到一个真正的答案(并使用相关设置的当前位置进行更新),这样就可以更容易地找到它(对于新生成的nest项目,这对我来说是现成的,无需更改任何其他配置或文件):
如果您转到设置>扩展>节点调试
并查找设置调试›节点:自动附加
并将其打开,则当您在集成终端中运行npm run start:debug
(即nest start--debug--watch
)时,VSCode将自动附加。适用于使用:
Launch.json(由提供)
终端
ng serve nestjs_project --port 9229
不需要搞乱
.vscode/launch.json
,只需遵循官方命令并。。。真管用
例如,我想调试我的项目测验:
将“自动连接”切换到“打开”
像往常一样运行应用程序,因为此项目是npm运行服务器:dev
应用程序成功启动后,连接到进程
看到第一个了吗?点击它!完成!现在您可以单击断点并检查李>
如果您希望在终端+所有调试功能中有日志输出,可以使用npm启动+附加,下面是针对launch.json的配置:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Nest Framework",
"args": ["${workspaceFolder}/src/main.ts"],
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector"
}
]
}
{
"type": "node",
"request": "launch",
"name": "Nest Debug",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"start:debug",
"--",
"--inspect-brk"
],
"console": "integratedTerminal",
"restart": true,
"protocol": "auto",
"port": 9229,
"autoAttachChildProcesses": true
},
如果出现这样的错误,将在console+调试中显示完整的日志输出
"debug": "nodemon --config nodemon-debug.json"
无法读取节点\模块/typescript/lib的源映射/
typescript.js
将“类型”设置为“pwa节点”而不是“节点”
如果你遇到这样的错误
"debug": "nodemon --config nodemon-debug.json"
找不到模块src/interceptors/auth.interceptor
将“runtimeArgs”设置为
[
"--nolazy",
"-r",
"ts-node/register",
"-r",
"tsconfig-paths/register"
]
而不是
[
"--nolazy", "-r", "ts-node/register"
]
结果:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Debug Nest Framework",
"args": ["${workspaceFolder}/src/main.ts"],
"runtimeArgs": [
"--nolazy",
"-r",
"ts-node/register",
"-r",
"tsconfig-paths/register"
],
"sourceMaps": true,
"cwd": "${workspaceRoot}"
// "protocol": "inspector"
}
],
"resolveSourceMapLocations": ["${workspaceFolder}/**", "!**/node_modules/**"]
}
但是我使用这个配置
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Nest Framework",
"args": [
"${workspaceFolder}/src/main.ts"
],
"runtimeArgs": [
"--nolazy",
"-r",
"ts-node/register",
"-r",
"tsconfig-paths/register"
],
"sourceMaps": true,
"envFile": "${workspaceFolder}/.env",
"cwd": "${workspaceRoot}",
"console": "integratedTerminal",
"protocol": "inspector"
},
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": ["--inspect-brk", "${workspaceRoot}/node_modules/.bin/jest", "--runInBand", "--coverage", "false"],
"console": "integratedTerminal"
}
]
}
感谢这个网站:所以我想你已经确定了所有的依赖项都在那里,确保你所有的vs代码插件都是更新的。有时一个错误的插件会导致vs代码出现这样的错误,vs代码和所有插件都是最新的。我认为这与“相对导入”有关。当我从“/players/players.module”使用导入{PlayersModule}时代码>而不是从“players/players.module”导入{PlayersModule}代码>它工作得很好。这就是对我有效的方法:下面是对我有效的分步说明:很好,但是在使用模块别名时还需要注册tsconfig路径,例如:“runtimeArgs”:[“--nolazy”、“-r”、“ts node/register”、“-r”、“tsconfig path/register”],否则调试器将大叫“找不到模块…”。如果你同意,请更新你的答案好吗。我对这一回应投了赞成票。但是,它不会在监视模式下运行应用程序。有没有办法在监视模式下运行它?要在监视模式下运行应用程序,请运行此命令->npm run start:dev
。你会