简体   繁体   中英

How to debug a Node-Red node with VS Code on Raspberry Pi?

I'm developing a Node-Red node for i2c devices, but can not debug the code, because it does not stop at breaking points.
Both Node-Red (2.1.3) + Visual Studio Code (v1.61.1) are installed on the same Raspberry Pi OS (32 bit).

I do not want to debug the whole NR system, nor node.js, just only that node (.js file) I'm working on inside VSCode and see the errors inside VSCode. (Not inside a browser's debugger.)

I've tried many many methods to adjust launch.json + package.json , added debugging lines into the.js file, etc.
I found two configs, which can start Node-red without errors, but it never stops anywhere during run.

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>/**"
            ]
          }        
    ]
}

I don't normally use Visual Code but I think I've got this working.

  • I'm assuming you are using the globally installed Node-RED

  • I have a directory on the remote pi with the node I'm developing /home/pi/test

  • In the /home/pi/.node-red I ran npm install /home/pi/test to install the node. (This will symlink the dev directory into the node_modules directory)

  • Run the following command to find the location of the Node-RED entry point:

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

    This is then used in the next step

  • Add the node-red entry to the scripts section of the node's package.json file:

     { "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" }
  • Set break points where you want them in the node's js file eg test.js .

  • Use the "Debug Script" to launch the node-red script from the package.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM