Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Flow to capture all node modules installed

Count of total modules + list of module names

If you want to know which node modules you have installed, the answer is in the ~/.node-red/.config.json file.

So we can easily grab that data and summarise it using the marvelous JSONata:

payload.nodes {
    "TOTAL": $count($keys($)),
    "node_modules": $keys($)
}

Below is an example flow. Note the example has two file read's to illustrate Linux and Windows file paths. You will need to adjust the file paths to suit your own installation.

List of module names with their node names

This JSONata will give you a list of objects where each object contains the name of the module and a list of the node names for that module.

(
    $each(payload.nodes,function($v, $k) {
        {
            "name": $k,
            "nodes": $keys($v.nodes)
        }
    }); 
)

Which gives output that looks like this (I've removed the node-red module as it is long):

[
  {
    "name": "node-red-node-twitter",
    "nodes": [
      "twitter"
    ]
  },
  {
    "name": "node-red-contrib-uibuilder",
    "nodes": [
      "uibuilder"
    ]
  }
]

Or, more compactly:

$each(payload.nodes,function($v, $k) {
    {$k: $keys($v.nodes)}
})

which gives:

[
  ...
  {
    "node-red-node-google": [
      "google plus",
      "google places",
      "google-api-config",
      "google geocoding",
      "google directions",
      "google calendar"
    ]
  },
  {
    "node-red-contrib-blockly": [
      "blockly"
    ]
  },
  ...
]

A more compact alternative would be:

$sort(payload.nodes.*.nodes.(
    $each($, function($v, $k) {
    	$k & " : " & $v.module
    })
))

Gives a list of nodes sorted by node name and each node shows the module it comes from.

[
  ...
  "file : node-red",
  "function : node-red",
  "google calendar : node-red-node-google",
  "google directions : node-red-node-google",
  "google geocoding : node-red-node-google",
  "google places : node-red-node-google",
  "google plus : node-red-node-google",
  "google-api-config : node-red-node-google",
  "httpin : node-red",
  "httprequest : node-red",
  "humanizer : node-red-contrib-moment",
  ...
]
[{"id":"57f9f3a.e32620c","type":"file in","z":"d480ffdb.73b02","name":"","filename":"/home/pi//node/nrlive/.data/.config.json","format":"utf8","chunk":false,"sendError":false,"x":470,"y":1040,"wires":[["d1d4a711.9a25d8"]]},{"id":"d1d4a711.9a25d8","type":"json","z":"d480ffdb.73b02","name":"","pretty":false,"x":690,"y":1040,"wires":[["e1bdf411.316238"]]},{"id":"e1bdf411.316238","type":"change","z":"d480ffdb.73b02","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.nodes {\t \"TOTAL\": $count($keys($)),\t \"node_modules\": $keys($)\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":860,"y":1040,"wires":[["1a0235f0.8c486a"]]},{"id":"1a0235f0.8c486a","type":"debug","z":"d480ffdb.73b02","name":"","active":true,"console":"false","complete":"false","x":1050,"y":1040,"wires":[]},{"id":"b6c696f6.b0bbe8","type":"inject","z":"d480ffdb.73b02","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":240,"y":1040,"wires":[["57f9f3a.e32620c","62b31849.ce3328"]]},{"id":"bae74244.be8fa","type":"comment","z":"d480ffdb.73b02","name":"How Many Node modules in my configuration?","info":"","x":350,"y":1000,"wires":[]},{"id":"62b31849.ce3328","type":"file in","z":"d480ffdb.73b02","name":"","filename":"C:\\src\\nr\\data\\.config.json","format":"utf8","chunk":false,"sendError":false,"x":430,"y":1080,"wires":[["d1d4a711.9a25d8"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment