# Subflow 2 Module helper flow

**URL:** <https://discourse.nodered.org/t/subflow-2-module-helper-flow/82763>\
**Category:** Share Your Projects\
**Tags:** subflow, module, package\
**Created:** [10 November 2023 17:22 UTC](https://discourse.nodered.org/t/subflow-2-module-helper-flow/82763 "2023-11-10T17:22:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [10 November 2023 17:22 UTC](https://discourse.nodered.org/t/subflow-2-module-helper-flow/82763/1 "2023-11-10T17:22:51Z")

</div>

Hey y'all

In the last month, I have seen a slight increase in the need to package subflows as modules,  
and the guide to do that is here : [Packaging a Subflow as a module : Node-RED](https://nodered.org/docs/creating-nodes/subflow-modules).

This is currently a manual affair.  
And one of the time consuming parts, is to manually make changes to the export of the subflow.

This small, no-fuss flow, should help address that!

Simply, paste your subflow export (please see guide, and I suggested using the `formatted` option) into the **Paste JSON In Here** template node, and click **Generate**.

The debug will then print the output that needs to be used in the **subflow.json** file (as explained in the guide linked above)

Anyway, it's just a thing I quickly put together, but should do the trick.  
Please note: This was created (and tested) in Node RED **3.1.0** , so just a reminder, it might not be compatible with earlier versions 🤷‍♂️

Enjoy & Happy Friday

```auto
[{"id":"b7329b63e35ef370","type":"tab","label":"Subflow To Module Helper","disabled":false,"info":"","env":[]},{"id":"5ad4c1667cdee07f","type":"inject","z":"b7329b63e35ef370","name":"Generate","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":145,"wires":[["8289fb654090dedd"]]},{"id":"0c8644b1b01426f4","type":"function","z":"b7329b63e35ef370","name":"Fix me up baby!","func":"let parsedObject = msg.payload\n\nparsedObject = parsedObject.filter((n) => !n.type.startsWith(\"subflow:\"))\n\nconst nodes = parsedObject.filter((n) => n.type !== 'subflow')\nconst subflow = parsedObject.filter((n) => n.type === 'subflow')[0]\n\nmsg.payload = {\n ...subflow,\n flow:[...nodes]\n}\n\nreturn msg","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":215,"y":275,"wires":[["469ac5aae85ef83f"]]},{"id":"8289fb654090dedd","type":"template","z":"b7329b63e35ef370","name":"Paste JSON In Here","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"","output":"json","x":225,"y":210,"wires":[["0c8644b1b01426f4"]]},{"id":"469ac5aae85ef83f","type":"debug","z":"b7329b63e35ef370","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":445,"y":275,"wires":[]}]

```

 ![Screenshot 2023-11-10 at 17.21.21](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/daf3a168bc12836f41d23dbca4d482f9a2880ba5.png)

2024-04-09 Flow updated as described in post #7

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 March 2024 14:25 UTC](https://discourse.nodered.org/t/subflow-2-module-helper-flow/82763/2 "2024-03-30T14:25:07Z")

</div>

Thank for this Marcus. Very useful.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [30 March 2024 18:55 UTC](https://discourse.nodered.org/t/subflow-2-module-helper-flow/82763/3 "2024-03-30T18:55:53Z")

</div>

Anytime!

It should still be current (hopefully) - was tested of course in 3.1

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 March 2024 20:56 UTC](https://discourse.nodered.org/t/subflow-2-module-helper-flow/82763/4 "2024-03-30T20:56:47Z")

</div>

Yes, it is working well with 4.0.0 Beta 1.  
By adding a JSON node with Format JSON String selected and a file out node then it can generate the subflow.json file directly.

---

<div class="post-metadata">

**Author:** ![nileio](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nileio/32/9444_2.png) [@nileio](https://discourse.nodered.org/u/nileio)\
**Post date:** [9 April 2024 13:20 UTC](https://discourse.nodered.org/t/subflow-2-module-helper-flow/82763/5 "2024-04-09T13:20:35Z")

</div>

@marcus-j-davies note there is a nasty issue which I am sure is an oversight - must admit it took me a bit to figure out what it was ! nothing to do with function code, but because you are using a `template` node and you left the default `Format` as `Mustache template`, it triggers replacements of special mustache braces ! so this means if your own subflow using template nodes and have those braces, the node **Paste JSON In Here** would attempt to substitute variables which of course won't be found, and will result on empty values in resulting output !! will then mean your node will behave very strange until you exhaust every path to debug, then think hmmm where is that issue coming from until you realise the template nodes in the subflow behaving strange compared to using it as just simply a subflow within a flow, and then voilà ! of course simply changing the `Format` of **Paste JSON In Here** `template` to `Plain Text` would resolve this ! I know it's long story but hey it drove me nuts to find cause it forced me to re-test every part of my subflow !! so gotta say it surely did **fix me up baby!** 😁

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 April 2024 13:48 UTC](https://discourse.nodered.org/t/subflow-2-module-helper-flow/82763/6 "2024-04-09T13:48:58Z")

</div>

I think you definitely deserve the 'Debugger of the Month` award for finding that. I can imagine it was a nightmare to track down.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [9 April 2024 17:18 UTC](https://discourse.nodered.org/t/subflow-2-module-helper-flow/82763/7 "2024-04-09T17:18:36Z")

</div>

Nice Spot @nileio🙇‍♂️

@Colin, can you edit with the below? - I dont have the rights being it's been too long.

updates:

- Set to Plain Text
- Output as JSON

```auto
[{"id":"b7329b63e35ef370","type":"tab","label":"Subflow To Module Helper","disabled":false,"info":"","env":[]},{"id":"5ad4c1667cdee07f","type":"inject","z":"b7329b63e35ef370","name":"Generate","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":145,"wires":[["8289fb654090dedd"]]},{"id":"0c8644b1b01426f4","type":"function","z":"b7329b63e35ef370","name":"Fix me up baby!","func":"let parsedObject = msg.payload\n\nparsedObject = parsedObject.filter((n) => !n.type.startsWith(\"subflow:\"))\n\nconst nodes = parsedObject.filter((n) => n.type !== 'subflow')\nconst subflow = parsedObject.filter((n) => n.type === 'subflow')[0]\n\nmsg.payload = {\n ...subflow,\n flow:[...nodes]\n}\n\nreturn msg","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":215,"y":275,"wires":[["469ac5aae85ef83f"]]},{"id":"8289fb654090dedd","type":"template","z":"b7329b63e35ef370","name":"Paste JSON In Here","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"","output":"json","x":225,"y":210,"wires":[["0c8644b1b01426f4"]]},{"id":"469ac5aae85ef83f","type":"debug","z":"b7329b63e35ef370","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":445,"y":275,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [9 April 2024 19:56 UTC](https://discourse.nodered.org/t/subflow-2-module-helper-flow/82763/8 "2024-04-09T19:56:01Z")

</div>

Flow updated

---

<div class="post-metadata">

**Author:** ![nileio](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nileio/32/9444_2.png) [@nileio](https://discourse.nodered.org/u/nileio)\
**Post date:** [10 April 2024 11:14 UTC](https://discourse.nodered.org/t/subflow-2-module-helper-flow/82763/9 "2024-04-10T11:14:11Z")

</div>

> [@Colin](#):
>
> I think you definitely deserve the 'Debugger of the Month` award for finding that. I can imagine it was a nightmare to track down.

Oh yeah sure did!! was a thorough mind training !! 😄
