node.js - 如何在 VSCode 中调试 nodemon 项目

标签 node.js debugging visual-studio-code nodemon vscode-debugger

我有一个 NodeJs 项目,我使用 nodemon 运行它,
我希望在 Debug模式下运行它来执行开发任务,但我无法这样做。

我发现我需要将正确的配置添加到 .vscode 文件夹下的 launch.json 文件中,
我有一个 app.js 文件,它是主应用程序文件。
该应用程序在 Node 版本 4.6.2端口 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 中需要进行哪些更改,或者启动配置 - launch.json 中需要进行哪些更正,才能帮助我在 VSCode 中针对我的用例调试应用程序?

最佳答案

将 package.json 更改为

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

--inspect is for versions >= 6.3. --legacy or --auto for older versions

并将 launch.json 发送到:

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

重启标志是这里的关键。

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

npm run debug

  • 在“调试” View 中,选择 Node :Nodemon 配置并按“播放” 或F5
  • 选择上面启动的流程

查看更多:vscode nodemon recipe

关于node.js - 如何在 VSCode 中调试 nodemon 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53352303/

相关文章:

jquery - node.js - 如何使用 jsdom/node-jquery 更新我的页面的外观?

git - VS 代码删除我的 Unity 项目 (OS X)(git clean all)

typescript - Visual Studio Code - 在所有 ES6 导入中应用重命名文件重构

node.js - 在 Swig 模板中使用 include 时连接变量和文件

javascript - 使用 Express.js 从 Angular.js 下载文件

c++ - 某些情况下 "disappears"的调试功能

java - 是否可以在 Eclipse 调试器的值列中显示 toString() 结果?

python - 尽管我安装了模块,但在 VS 代码中未发现错误

node.js - 依赖项 - 未安装错误(npm WARN optional SKIPPING OPTIONAL DEPENDENCY : fsevents@^1. 0.0 (node_modules\chokidar\node_modules\fsevents)

javascript - 是否有任何 Javascript 调试 API?