使用vscode调试nest.js应用程序

ken*_*tor 9 typescript

我正在测试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)

终于成功了!

  • 如果您进入 Settings =&gt; Debug › Node: Auto Attach 并将其打开,当您在集成终端中运行 `npm run start:debug` 时,VSCode 将自动附加。 (12认同)
  • 效果很好,谢谢!我更喜欢附加到正在运行的实例,因为我发现它的侵入性较小。 (2认同)

ken*_*ley 25

无需搞砸.vscode/launch.json,只需按照官方的自动附加介绍进行操作即可......正常工作!

例如,我想调试我的项目quiz

  1. 将“自动附加”切换为“开”

切换自动附加

  1. 像往常一样运行应用程序,因为这个项目是npm run server:dev

  2. 当应用程序成功启动后,附加到进程

附加到节点进程

  1. 看到第一个了吗?点击它!完毕!现在您可以单击断点并进行检查!

选择流程

  • 启用自动附加后需要重新启动终端 (7认同)
  • 极好的!即使只完成第一步,这对我也有效。我的 Nestjs 应用程序已经在调试模式下启动(npm run start:debug)。因此,按下 Ctrl+Shift+P 并输入“自动附加”,然后选择,瞧,我可以设置断点和调试器语句了!谢谢! (4认同)
  • 这应该标记为已接受的答案。多谢。 (3认同)

小智 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)

  • @Saurabh47g 不清楚。如何从 launch.json 使用 npm run start:dev ? (5认同)
  • 很好,但是使用模块别名时还需要注册tsconfig-paths,例如:“ runtimeArgs”:[“ --nolazy”,“-r”,“ ts-node / register”,“-r”,“ tsconfig -paths / register“],否则调试器会大喊“找不到模块...”。如果您同意,请更新您的答案吗? (3认同)
  • 有效。我对这个回应投了赞成票。但是,它不会在监视模式下运行应用程序。有什么办法可以在手表模式下运行它吗? (3认同)

cha*_*avy 6

这个设置对我有用

{
  "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)

从这里获取


San*_*air 5

这是一个有效的配置。希望这对某人有所帮助:)

确保在下面添加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)


Jas*_*idi 5

对于使用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)

将在控制台 + 调试中显示完整的日志输出

  • 这是迄今为止我为 NestJS 新用户找到的开箱即用的最佳解决方案。这会编译、调试并监视我的项目中文件的更改。对于刚刚学习 NestJS 的人来说,其他一切都错过了其中一个。 (3认同)

小智 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)

这将以调试模式启动应用程序并附加调试器