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

时间:2018-03-27 05:09:08

标签: typescript

我正在测试nest.js框架,但我正在努力用VSCode运行它,以便我可以正确调试我的代码。这与此处Running nest.js from VS Code所述的问题几乎相同。但是我确定我使用的是最新的软件包。我总是得到这个错误:

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)

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"
  },

我的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"
            ]
        }
    ]
}

我尝试使用typescript文件作为路径使用相同的launch.json,但是它引发了相同的异常:

"program": "${workspaceFolder}\\src\\main.ts",

12 个答案:

答案 0 :(得分:13)

尝试此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"
        }
    ]
}

答案 1 :(得分:8)

无需弄乱.vscode/launch.json,只需遵循官方的Auto Attach intro并...就可以了!

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

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

Toggle Auto Attach

  1. 照常运行该应用,因为该项目为npm run server:dev

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

Attach to Node process

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

Pick the process

答案 2 :(得分:4)

我尝试了接受的答案以及所有其他差异,但没有一个对我有用,但是对我真正有用的是附加到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,请参阅此blog

{
  "name": "nest-app",
  "scripts": {
    "start:debug": "nest start --debug --watch"
  }
}

终于可以了!

答案 3 :(得分:4)

对于使用Nrwl的人:

Launch.json (由@ghiscoding提供)

{
      "type": "node",
      "request": "attach",
      "name": "Attach NestJS WS",
      "port": 9229,
      "restart": true,
      "stopOnEntry": false,
      "protocol": "inspector"
    }

终端

ng serve nestjs_project --port 9229

答案 4 :(得分:2)

我只需要通过@JWess来“推广” this comment到一个真实的答案(并使用相关设置的当前位置更新它),这样就可以更容易地找到它(这对于框,用于新建的Nest项目,而无需更改任何其他配置或文件):

如果转到Settings > Extensions > Node debug并查找设置Debug › Node: Auto Attach并将其打开,则当您在npm run start:debug(即nest start --debug --watch)中运行x.withColumn("A", when ($"B" === "apple", "fruit").otherwise(col("C"))) (即val y = x.withColumn("A", when ($"B" === "apple", "fruit").otherwise(col("B"))) )时,VSCode将自动附加集成终端。

答案 5 :(得分:1)

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

确保在tsconfig-paths/register下添加runtimeArgs行,否则将收到一条错误消息,提示未找到某些用户定义的模块。

如果在根项目文件夹下有一个<YOUR_APP_ROOT_FOLDER>名称,请用应用程序文件夹名称替换,否则从脚本路径中将其删除。

注意::请确保在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"
        }
    ]
}

答案 6 :(得分:1)

如果要在终端中具有日志输出+所有调试功能,则可以使用npm启动+附加,这是launch.json的配置:

i18n.t

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

答案 7 :(得分:1)

如果你遇到这样的错误

<块引用>

无法读取 node_modules / typescript / lib / 的源映射 打字稿.js

"type" 设置为 "pwa-node" 而不是 "node"

如果你遇到这样的错误

<块引用>

找不到模块 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"
    }
  ]
}

感谢本网站:Click me

答案 8 :(得分:0)

在src外部创建一个index.js文件(注意,该文件夹不在SRC文件夹中,只是在外面具有package.json文件)文件夹,其中包含以下内容

require('ts-node/register');
require('./src/main');

然后在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": "Launch Program",
        "program": "${workspaceFolder}\\index.js",
        "autoAttachChildProcesses": true,
        "preLaunchTask": "tsc: build - tsconfig.json",
        "outFiles": [
            "${workspaceFolder}/dist/**/*.js"
        ]
    }
]
}

答案 9 :(得分:0)

此设置对我有用

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

here

获取

答案 10 :(得分:0)

我所做的是使用package.json中的脚本之一自动附加vs代码调试过程。最重要的是,我使用了nodemon,如果您对开发进行了任何更改,它将与调试器一起自动重新启动。

此过程要求您全局安装nodemon,并在文件夹的根目录中添加一个nodemon-debug.json文件,如下所示。

nodemon-debug.json

$("#hideAccordianCards").click(function() { $('.collapse').removeClass('show'); });
$("#showAccordianCards").click(function() { $('.collapse').addClass('show'); });

然后在package.json中,添加一个如下所示的脚本

{
    "watch": ["src"],
    "ext": "ts",
    "ignore": ["src/**/*.spec.ts"],
    "exec": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register src/main.ts"
}

然后在VS代码中,按 F1 >搜索 Debug:Toggle Auto Attach 。点击它以启用它。

然后通过运行以下命令启动调试过程-

"debug": "nodemon --config nodemon-debug.json"

调试器会自动打开。

此过程的优势是nodemon,它会在您每次对代码进行一些更改并需要它时自动与调试器一起启动。

有关更详细的说明,请阅读此link。为我工作。

答案 11 :(得分:0)

我在 Nx Workspace(Nx 版本 11.6.3)中使用 NestJs 应用程序,它在不使用任何 launch.json 设置的情况下对我有用。

在 VS Code 中,我刚刚启用了 Auto Attach > Smart,然后我使用以下命令运行了 NestJS 应用程序:
nx serve <nest-js-app-name> --inspect=inspect --port=9229
并且 VS Code 会自动附加调试器。

我的 launch.json 有一个配置来启动我的前端应用程序,而当您启动 NestJS 应用程序时,上述过程会自动附加调试器。这样我就同时启动了前端和后端进行调试。