我正在测试nest.js框架,但我很难用VSCode运行它,以便我可以正确调试我的代码.这与此处描述的问题几乎相同.从VS Code运行nest.js.但是我确定我使用的是最新的软件包.我总是得到这个错误:
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)
Run Code Online (Sandbox Code Playgroud)
npm run start 工作完全完美,但我想用VSCode IDE调试应用程序.
我的package.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"
},
Run Code Online (Sandbox Code Playgroud)
我的vscode的launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\dist\\main.js",
"smartStep": true,
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用typescript文件作为路径使用相同的launch.json,但是它引发了同样的异常:
"program": "${workspaceFolder}\\src\\main.ts",
Run Code Online (Sandbox Code Playgroud)
ghi*_*ing 39
我尝试了接受的答案和所有其他差异,但没有一个对我有用,但是真正对我有用的是连接到 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"
}
]
}
Run Code Online (Sandbox Code Playgroud)
并在package.json(嵌套新的 CLI 命令,需要6.8.x,请参阅此博客)
{
"name": "nest-app",
"scripts": {
"start:debug": "nest start --debug --watch"
}
}
Run Code Online (Sandbox Code Playgroud)
终于成功了!
ken*_*ley 25
无需搞砸.vscode/launch.json,只需按照官方的自动附加介绍进行操作即可......正常工作!
例如,我想调试我的项目quiz:
像往常一样运行应用程序,因为这个项目是npm run server:dev
当应用程序成功启动后,附加到进程
小智 22
试试这个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"
}
]
}
Run Code Online (Sandbox Code Playgroud)
这个设置对我有用
{
"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"
}
Run Code Online (Sandbox Code Playgroud)
从这里获取
这是一个有效的配置。希望这对某人有所帮助:)
确保在下面添加tsconfig-paths/register一行,runtimeArgs否则您将收到一条错误消息,指出未找到某些用户定义的模块。
<YOUR_APP_ROOT_FOLDER>如果您的根项目文件夹下有应用程序文件夹名称,也将name替换为您的应用程序文件夹名称,否则将其从脚本中的路径中删除。
注意:在 vscode 上执行此调试配置之前,请确保停止运行您的应用程序,因为此调试脚本将在同一端口上启动您的应用程序的新实例。
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Nest Framework",
"args": [
"${workspaceFolder}/<YOUR_APP_ROOT_FOLDER>/src/main.ts"
],
"runtimeArgs": [
"--nolazy",
"-r",
"ts-node/register",
"-r",
"tsconfig-paths/register"
],
"sourceMaps": true,
"cwd": "${workspaceRoot}/<YOUR_APP_ROOT_FOLDER>",
"protocol": "inspector"
}
]
}
Run Code Online (Sandbox Code Playgroud)
对于使用Nrwl 的人:
Launch.json(由@ghiscoding 提供)
{
"type": "node",
"request": "attach",
"name": "Attach NestJS WS",
"port": 9229,
"restart": true,
"stopOnEntry": false,
"protocol": "inspector"
}
Run Code Online (Sandbox Code Playgroud)
终端
ng serve nestjs_project --port 9229
Run Code Online (Sandbox Code Playgroud)
小智 5
如果你想在终端输出日志+所有调试功能,你可以使用npm启动+附加,这是launch.json的配置:
{
"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
},
Run Code Online (Sandbox Code Playgroud)
将在控制台 + 调试中显示完整的日志输出
小智 5
将此配置添加到 launch.json
{
"name": "npm run start:debug",
"skipFiles": ["<node_internals>/**"],
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run", "start:debug"],
"port": 9229,
"env": {
"PORT": "5000" // env var used in app
}
},
Run Code Online (Sandbox Code Playgroud)
这将采用监视模式,因此无论对.ts文件进行什么更改,它都会立即反映,并确保 package.json 中有以下脚本
"start:debug": "nest start --debug --watch"
Run Code Online (Sandbox Code Playgroud)
这将以调试模式启动应用程序并附加调试器
| 归档时间: |
|
| 查看次数: |
6281 次 |
| 最近记录: |