debugging 如何在VSCode中调试nodemon项目

rkue9o1l  于 2022-11-24  发布在  Vscode
关注(0)|答案(9)|浏览(195)

我有一个NodeJs项目,
我希望在调试模式下运行它以执行开发任务,但我无法这样做。
我发现需要将正确的配置添加到.vscode文件夹下的launch.json文件中,
我有一个app.js文件,这是主要的应用程序文件。
应用程序在node version 4.6.2Port 8080上运行。
在通常情况下,我使用npm run dev命令运行应用程序。

以下是我的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": "MyApp",
            "program": "${workspaceFolder}/app.js",
            "runtimeVersion": "4.6.2",
            "protocol": "legacy",
            "port": 8080
            //"runtimeExecutable": "/home/user/.nvm/versions/node/v4.6.2/bin/node"
        },
        {
            "type": "node",
            "request": "launch",
            "name": "nodemon",
            "runtimeExecutable": "nodemon",
            "program": "${workspaceRoot}/app.js",
            "restart": true,
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "runtimeVersion": "4.6.2",
            "protocol": "legacy",
            "port": 8080
        },
        {
            "type": "node",
            "request": "launch",
            "name": "DEBUG",
            "runtimeExecutable": "nodemon",
            "program": "${workspaceFolder}/app.js",
            "restart": true,
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "runtimeVersion": "4.6.2",
            "protocol": "legacy",
            "port": 8080
        }
    ]
}

package.json如下所示-

{
  "name": "myapp",
  "description": "myapp",
  "version": "1.35.0",
  "private": true,
  "scripts": {
    "dev": "nodemon app.js",
    "debug": "nodemon app.js"
  },
  "dependencies": {
    "async": "1.3.0",
    "aws-sdk": "2.7.20",
    "aws-xray-sdk": "^2.1.0",
    "aws-xray-sdk-restify": "^1.3.0-beta",
    "bcrypt": "0.8.5",
    "body-parser": "1.12.3",
    "compression": "^1.7.0",
    "connect-flash": "0.1.1",
    "cookie-parser": "1.3.4",
    "cron": "1.0.9",
    "csurf": "^1.9.0",
    "csvtojson": "^1.1.2",
    "date-utils": "1.2.16",
    "dotenv": "4.0.0",
    "email-templates": "1.2.1",
    "express": "4.12.3",
    "express-handlebars": "2.0.0",
    "express-jwt": "^5.1.0",
    "express-mailer": "0.2.4",
    "express-session": "1.11.1",
    "express-validator": "3.1.3",
    "handlebars": "^3.0.3",
    "helmet": "^3.5.0",
    "html-pdf": "1.4.0",
    "json-2-csv": "2.0.12",
    "jsonwebtoken": "^7.3.0",
    "multer": "^0.1.8",
    "mysql": "2.6.2",
    "newrelic": "1.25.0",
    "node-schedule": "^1.3.0",
    "nodemailer": "^1.3.4",
    "nodemailer-ses-transport": "1.2.0",
    "passport": "0.2.1",
    "passport-local": "1.0.0",
    "path": "0.11.14",
    "promise": "7.0.0",
    "qs": "^2.4.1",
    "replaceall": "0.1.6",
    "request": "2.55.0",
    "run-parallel": "1.1.0",
    "validator": "^7.0.0",
    "winston": "^2.3.1",
    "winston-daily-rotate-file": "^1.7.0",
    "xlsx": "0.8.8"
  },
  "devDependencies": {
    "nodemon": "^1.17.3"
  }
}

**当我运行DEBUG和nodemon配置时,应用程序启动,

但是代码在我放在app.js文件上的断点处没有暂停。**

参考链接-

  1. https://github.com/Microsoft/vscode-recipes/tree/master/nodemon
  2. https://github.com/bdspen/nodemon_vscode
  3. Can Visual Studio Code be configured to launch with nodemon
  4. Cannot debug in VSCode by attaching to Chrome
  5. https://code.visualstudio.com/docs/editor/debugging
    在package.json中需要做哪些更改,或者在Launchconfiguration-launch.json中需要做哪些更正,这将有助于我在VSCode中为我的用例调试应用程序?
fnvucqvd

fnvucqvd1#

Change package.json to

"scripts": {
    "dev": "node app.js",
    "debug": "nodemon --inspect app.js"
}

--inspect is for versions >= 6.3. --legacy or --auto for older versions
And launch.json to:

"version": "0.2.0",
"configurations": [
    {
        "type": "node",
        "request": "attach",
        "name": "Node: Nodemon",
        "processId": "${command:PickProcess}",
        "restart": true,
        "protocol": "inspector"
    }
]

the restart flag is the key here.
Start app via new debug script
npm run debug

  • In Debug view, select the Node: Nodemon configuration and press play or F5
  • Choose the process started above

See more: vscode nodemon recipe

cvxl0en2

cvxl0en22#

在vscode配置中,您可以设置runtimeExecutable,它将运行您给定的程序。设置restart:true以便vs代码调试器可以重新启动进程。
这是一个配置示例:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node", 
            "request": "launch",
            "name": "nodemon",
            "runtimeExecutable": "nodemon",
            "program": "${workspaceFolder}/bin/www",
            "restart": true,
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "env": {
                "debug": "app:*",
            }
        }
    ]
}

program更新为要调试的节点文件。
这比将调试器附加到正在运行的节点进程要容易。

wmtdaxz3

wmtdaxz33#

我在一个Dockerized nodemon进程中遇到了类似的问题。我在this article中找到了解决方案。我能够通过更改三件事来让它工作:
1.将--inspect=0.0.0.0添加到启动服务的npm脚本(在我的示例中命名为debug):

"scripts": {
    "debug": "nodemon -w lib -w server.js --inspect=0.0.0.0 server.js"
  }

1.确保端口9229(或者您希望使用的任何调试端口)在Docker容器中打开。我使用的是docker-compose,因此它在关联的yaml中定义:

ports:
  - "8080:8080"
  - "9229:9229"

1.将以下配置添加到VS代码中的launch.json

"configurations": [
        {
            "name": "Attach to Node in Docker",
            "type": "node",
            "address": "localhost",
            "port": 9229,
            "request": "attach",
            "restart": true
        }
    ]

"restart": true选项允许调试器在nodemon在监视的文件更改后重新编译内容时自动重新附加。

8i9zcol2

8i9zcol24#

您可以使用F5启动并连接nodemon,但是需要进行更多的设置。
我们必须首先通过VS代码任务预启动nodemon,然后附加。
我使用远程调试器进行附加,因为它不需要额外的点击来选择要附加的进程,并且VS Code进程选择器当前是broken in WSL2,这是我的主开发环境。
tasks.json(来自this answer):

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "npm dev",
      "type": "npm",
      "script": "dev",
      "isBackground": true,

      // This task is run before some debug tasks.
      // Problem is, it's a watch script, and since it never exits, VSCode
      // complains. All this is needed so VSCode just lets it run.
      "problemMatcher": [
        {
          "pattern": [
            {
              "regexp": ".",
              "file": 1,
              "location": 2,
              "message": 3
            }
          ],
          "background": {
            "activeOnStart": true,
            "beginsPattern": ".",
            "endsPattern": "."
          }
        }
      ]
    }
  ]
}

launch.json:

{
  "type": "node",
  "request": "attach",
  "name": "Launch & Attach",
  "restart": true,
  "localRoot": "${workspaceRoot}",
  "remoteRoot": "${workspaceRoot}",
  "preLaunchTask": "npm dev"
}

在npm dev脚本中(对于节点〉= 6.9):

nodemon --watch src -e js --exec node --inspect .

注意--如果进程启动时间超过10秒,这种方法就不起作用。在这种情况下,你必须弄清楚如何在预启动任务完成时向VS Code发出信号。这可能可以通过使用beginsPattern / endPattern正则表达式来实现,尽管我还没有尝试过。

m3eecexj

m3eecexj5#

在2022年,我用这个:
第一个
在此之后,我在vscode中单击Run and Debug: Launch via NPM。调试器将以nodemon启动,并且在代码更改时断点将正常工作。

92dk7w1h

92dk7w1h6#

正如“极客”所建议的,
1.您应该修改launch.json,如下所示:

{
    "version": "0.2.0",
    "configurations": 
  [    
    {
        "type": "node",
        "request": "attach",
        "name": "Attach by Process ID",
        "processId": "${command:PickProcess}"
    }
  ]
}

1.启动服务器“npm run dev”(如您所见,在“request”属性中,我们将其设置为attach。因此,我们必须先运行服务器,然后再连接调试器)。
1.点击vscode左边的类似bug的图标。在上面你会看到一个绿色的小播放图标。点击它右边的下拉箭头,然后选择“Attach by process ID”。
1.点击播放图标,然后vscode底部的一个条应该变成深橙色。现在尝试发出一个请求。断点将被正确命中!

qgzx9mmu

qgzx9mmu7#

nodemon监听文件更改并在另一个进程上重新启动应用程序
因此,您的配置是正确的,但调试器永远不会“看到”断点。
使用nodemon运行调试模式是没有意义的。
这是您可能希望在VScode上请求的功能(代码更改时自动重启)

wvyml7n5

wvyml7n58#

我在寻找同样的问题,并遇到了这个问题,因为现在在2022年7月1日,这似乎是最好的方法。使用自动附加功能在VSCode.
我按照本文所述,重新启动IDE,设置断点,然后使用以下命令运行代码:

nodemon app.js

一切都按预期正常工作。
所以过程:

Activating Auto Attach -> Restarting IDE -> Setting Break Points-> Running with Nodemon
wfveoks0

wfveoks09#

这里是2022
在我使用以下值编辑runtimeExecutable之前,没有一个解决方案对我有效:

"runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",

其给出:

{
  "name": "debug nodemon",
  "type": "node",
  "request": "launch",
  "runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
  "program": "${workspaceFolder}/app.js",
  "restart": true,
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen"
},

相关问题