如何在 VSCode 中调试 nodemon 项目

新手上路,请多包涵

我有一个 NodeJs 项目,我使用 nodemon 运行它,

我希望在调试模式下运行它以执行开发任务,但我无法这样做。

我发现我需要在 .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. Visual Studio Code 是否可以配置为使用 nodemon 启动

4. VSCode 无法通过附加到 Chrome 进行调试

5. https://code.visualstudio.com/docs/editor/debugging

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

原文由 Ani 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 953
1 个回答

将 package.json 更改为

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

–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"
    }
]

重启标志是这里的关键。

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

npm 运行调试

  • 在调试视图中,选择 节点:Nodemon 配置并按播放或 F5
  • 选择上面启动的进程

查看更多: vscode nodemon 配方

原文由 The Geek 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题