如何在VSCode中调试NodeMon项目

Ani*_*aje 3 debugging node.js nodemon visual-studio-code vscode-debugger

我有一个NodeJs项目,我使用nodemon运行它,
我希望以调试模式运行它以完成开发任务,但是我无法这样做。

我发现我需要在.vscode文件夹下的launch.json文件中添加正确的配置,
我有一个app.js文件是主应用程序文件。
应用程序将在node version 4.6.2和上运行Port 8080
在通常情况下,我使用npm run dev命令运行App 。

以下是我的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
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

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"
  }
}
Run Code Online (Sandbox Code Playgroud)

我在运行DEBUG和nodemon配置时启动了该应用程序,
但是代码没有在我放置在app.js文件上的断点处暂停。

参考链接-1
. https://github.com/Microsoft/vscode-recipes/tree/master/nodemon
2. https://github.com/bdspen/nodemon_vscode
3.是否可以将Visual Studio Code配置为与nodemon
4 一起启动无法通过连接到Chrome浏览器中VSCode调试
5. https://code.visualstudio.com/docs/editor/debugging

在package.json中需要进行哪些更改,或者在Launch配置中进行任何更正-launch.json,这将有助于我为用例在VSCode中调试应用程序?

Rid*_*ara 14

在 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:*",
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

更新program到要调试的节点文件。

这比将调试器附加到正在运行的节点进程容易。

  • 我知道我们不应该只是为了说“谢谢”而写评论,但非常感谢。我不知道我尝试了多少种不同的 launch.json 都没有成功,直到这一个!(不过,在我的例子中,我必须将“${workspaceFolder}/bin/www”更改为“${workspaceFolder}/server”)。 (3认同)

rot*_*ial 13

我在附加到 Dockerized nodemon 进程时遇到了类似的问题。我在这篇文章中找到了解决方案。我通过改变三件事来让它工作:

  1. 添加--inspect=0.0.0.0到启动服务的 npm 脚本(debug在我的例子中命名):
  "scripts": {
    "debug": "nodemon -w lib -w server.js --inspect=0.0.0.0 server.js"
  }
Run Code Online (Sandbox Code Playgroud)
  1. 确保端口 9229(或您想要使用的任何调试端口)在 Docker 容器中打开。我正在使用docker-compose,因此它在关联的 yaml 中定义:
ports:
  - "8080:8080"
  - "9229:9229"
Run Code Online (Sandbox Code Playgroud)
  1. 在 VS Code 中添加以下配置launch.json
    "configurations": [
        {
            "name": "Attach to Node in Docker",
            "type": "node",
            "address": "localhost",
            "port": 9229,
            "request": "attach",
            "restart": true
        }
    ]
Run Code Online (Sandbox Code Playgroud)

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


Але*_* К. 9

2022 年我用这个:

// launch.json
"configurations": [
        {
            "name": "Launch via NPM",
            "request": "launch",
            "runtimeArgs": ["run", "debug"],
            "runtimeExecutable": "npm",
            "skipFiles": ["<node_internals>/**"],
            "type": "node"
        }
    ]
Run Code Online (Sandbox Code Playgroud)
// package.json
"scripts": {
        "debug": "nodemon dist/app"
    }
Run Code Online (Sandbox Code Playgroud)

之后我点击Run and Debug: Launch via NPMvscode。调试器从 nodemon 开始,断点在代码更改时起作用。


小智 8

将package.json更改为

"scripts": {
    "dev": "node app.js",
    "debug": "nodemon --inspect app.js"
}
Run Code Online (Sandbox Code Playgroud)

--inspect适用于> = 6.3的版本。--legacy或--auto(对于较旧的版本)

并将launch.json转换为:

"version": "0.2.0",
"configurations": [
    {
        "type": "node",
        "request": "attach",
        "name": "Node: Nodemon",
        "processId": "${command:PickProcess}",
        "restart": true,
        "protocol": "inspector"
    }
]
Run Code Online (Sandbox Code Playgroud)

重新启动标志是这里的关键。

通过新的调试脚本启动应用

npm运行调试

  • 在“调试”视图中,选择“ 节点:Nodemon”配置,然后按play或F5
  • 选择上面开始的过程

查看更多:vscode nodemon食谱

  • 注意:您应该选择的进程不是nodemon,而是运行您的应用程序的node进程。 (2认同)

Emi*_*nge 7

在我runtimeExecutable使用以下值进行编辑之前,没有任何解决方案对我有用:

"runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
Run Code Online (Sandbox Code Playgroud)

这使:

{
  "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"
},
Run Code Online (Sandbox Code Playgroud)


Maz*_*516 1

nodemon 监听文件更改并在另一个进程上重新启动应用程序

因此,您的配置是正确的,但调试器永远不会“看到”断点。

使用 nodemon 运行调试模式是没有意义的。

这是您可能想要在 VScode 上请求的功能(代码更改时自动重新启动)