【问题标题】:How to debug a Node-Red node with VS Code on Raspberry Pi?如何在 Raspberry Pi 上使用 VS Code 调试 Node-Red 节点?
【发布时间】:2022-01-19 15:21:58
【问题描述】:

我正在为 i2c 设备开发 Node-Red 节点,但无法调试代码,因为它不会在断点处停止。
Node-Red (2.1.3) + Visual Studio Code (v1.61.1) 都安装在同一个 Raspberry Pi OS(32 位)上。

我不想调试整个 NR 系统,也不想调试 node.js,我只想在 VSCode 中处理 那个 节点(.js 文件)并查看 VSCode 中的错误。 (不在浏览器的调试器中。)

我尝试了很多方法来调整launch.json + package.json,在.js 文件中添加debugging 行等。
我找到了两个配置,可以无错误地启动 Node-red,但在运行期间它永远不会停止。

package.json:


  "scripts": {
    "inspect": "node --inspect /usr/lib/node_modules/node-red/red.js --userDir /home/pi/.node-red/node_modules/mcp-pfc-aio",
    "start": "node node_modules/node-red/red.js -v -u . -s settings.js",
    "debug": "node --nolazy --inspect-brk=9229 node_modules/node-red/red.js -v -u . -s settings.js"
  },

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "name": "Run Script: debug MCP3",
            "program": "/usr/lib/node_modules/node-red/red.js",
            "mode": "debug",
            "cwd": "/home/pi/.node-red/",
            "skipFiles": [
                "<node_internals>/**"
            ],
            //"runtimeExecutable": "npm",
            "runtimeArgs": ["--preserve-symlinks", "--experimental-modules"],
            "request": "launch"
            //"command": "npm run debug",
        },
        {
            "type": "node",
            "request": "launch",
            "mode": "debug",
            "name": "Launch via NPM",
            "runtimeExecutable": "npm",
            "runtimeArgs": [
                "--preserve-symlinks", "--experimental-modules",
                "run-script",
                "inspect"
            ],
            "program": "/usr/lib/node_modules/node-red/red.js",
            "stopOnEntry": true,
//            "cwd": "/home/pi/.node-red/",
//            "cwd": "${workspaceRoot}",
            "port": 9229,
            "skipFiles": [
              "<node_internals>/**"
            ]
          }        
    ]
}

【问题讨论】:

    标签: debugging visual-studio-code node-red


    【解决方案1】:

    我通常不使用 Visual Code,但我认为我已经完成了这项工作。

    • 我假设您使用的是全局安装的 Node-RED

    • 我在远程 pi 上有一个目录,其中包含我正在开发的节点 /home/pi/test

    • /home/pi/.node-red 中我运行npm install /home/pi/test 来安装节点。 (这会将 dev 目录符号链接到 node_modules 目录)

    • 运行以下命令查找 Node-RED 入口点的位置:

      $ readlink -f $(which node-red)
      /usr/lib/node_modules/node-red/red.js
      

      然后在下一步中使用它

    • node-red 条目添加到节点package.json 文件的脚本部分:

      {
        "name": "test",
        "version": "1.0.0",
        "description": "",
        "scripts": {
          "node-red": "node /usr/lib/node_modules/node-red/red.js"
        },
        "keywords": [
          "node-red"
        ],
        "node-red": {
          "nodes": {
            "test": "test.js"
          }
        },
        "author": "ben@example.com",
        "license": "Apache-2.0"
      }
      
    • 在节点的 js 文件中设置断点,例如test.js.

    • 使用“调试脚本”从package.json 启动node-red 脚本

    【讨论】:

    • 请将launch.json 配置也添加到您的答案中!从{} Node.js: ... 开头的 10 个配置示例中,您选择了哪一个?
    • 我没有创建任何launch.json文件,我只是告诉Visual Code“调试”node-red脚本(将鼠标悬停在package.json中的"node-red"上,然后弹出带运行/调试)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多