# Nodered, refactoring code and autocompletion

**URL:** <https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427>\
**Category:** General\
**Created:** [9 January 2024 15:05 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427 "2024-01-09T15:05:04Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![iznogoud320](https://avatars.discourse-cdn.com/v4/letter/i/5f9b8f/32.png) [@iznogoud320](https://discourse.nodered.org/u/iznogoud320)\
**Post date:** [9 January 2024 15:05 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/1 "2024-01-09T15:05:04Z")

</div>

Hello everyone,

I'm relatively new to nodered, and using it mainly for my home automation. I already learned a lot using this forum on another subject. I now have a question regarding refactoring code and autocompletions within nodered.

I'm at the point of having variables defined as flow variables in a flow that, in the end, I'd need to access from another flow. This should imply to have used my variables as global instead of flow.

I know that I can make searches on the var names for example and nodered returns all the instances where the result is found but it implies making manual changes on every instance.

1. Is there an elegant way to perform such changes quite automatically, like for each instance of 'myvar' replace all the flow.set or flow.get by global.get/set? Including in all nodes like switch nodes or so?

2. Is the option of modifying the flows.json file through an editor acceptable?

3. Is there also a possibility for the editor to propose some autocompletion based on context variables names?

Thanks for your help and suggestions.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [9 January 2024 15:09 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/2 "2024-01-09T15:09:27Z")

</div>

Hi,

For 3, it's done 😉 [Add auto-complete to flow/global typedInput types by knolleary · Pull Request #4480 · node-red/node-red · GitHub](https://github.com/node-red/node-red/pull/4480)

Be there soon 😋

---

<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 January 2024 15:12 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/3 "2024-01-09T15:12:14Z")

</div>

Option 2 might be the way…

1. Export all flows from the editor
2. Find/Replace (in the file that gets exported)
3. Re-import (you will have to remove old duplicates)

As said by @GogoVega version 3 does support auto complete, but that’s still in oven

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [9 January 2024 15:17 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/4 "2024-01-09T15:17:24Z")

</div>

Or you edit the `flows.json` file directly and restart NR 🤓

---

<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 January 2024 15:20 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/5 "2024-01-09T15:20:40Z")

</div>

You will have to make sure nothing is written to or modifies the flows file if going down that route.

I don’t know if something gets written whilst restarting 🤷‍♂️

Modifying the flows file whilst it’s running seems risky

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [9 January 2024 15:25 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/6 "2024-01-09T15:25:41Z")

</div>

> [@marcus-j-davies](#):
>
> Modifying the flows file whilst it’s running seems risky

This file is only modified when deploying (plus a difference), so you can modify it while NR is running without problem. Of course you can stop it, modify the file then restart the instance

I think you are confusing with the context files Marcus 😁

---

<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 January 2024 15:28 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/7 "2024-01-09T15:28:47Z")

</div>

No, I’m referring to making changes to the flows file that is in use by Node RED directly.

Personally I would be careful in doing so 🥴

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 January 2024 15:31 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/8 "2024-01-09T15:31:49Z")

</div>

> [@marcus-j-davies](#):
>
> No, I’m referring to making changes to the flows files that is in use by Node RED directly.

Yes, since the file is only loaded on Node-RED start, you can do this. Though the Editor might toss you a mr angry message. 🙂

> [@marcus-j-davies](#):
>
> Personally I would be careful in doing so

Oh so very YES. 🤣 Make sure you have a copy of the file. I've done it on occasion and yes, on occasion messed it up. 😖

---

<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 January 2024 15:35 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/9 "2024-01-09T15:35:44Z")

</div>

You’re all crazy!! 😨

Making changes to the live flows file by hand!  
As long as caution is practiced, I suppose I’m out numbered 😝

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [9 January 2024 15:39 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/10 "2024-01-09T15:39:18Z")

</div>

![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/9/6927f86d42b59f825f2e362db7bec13b4c4eb70d.gif "Willem Dafoe GIF - Willem Dafoe Laugh - Discover & Share GIFs")

> [@marcus-j-davies](#):
>
> As long as caution is practiced

Makes sense 😅

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [9 January 2024 16:03 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/11 "2024-01-09T16:03:38Z")

</div>

> [@iznogoud320](#):
>
> I'm relatively new to nodered,  
> I'm at the point of having variables defined as flow variables in a flow that, in the end, I'd need to access from another flow. This should imply to have used my variables as global instead of flow.

If you are moving towards more global variables, I suspect that you may be misunderstanding the nature of flow based programming. Instead of saving data as variables, you should try to attach it to each message as it passes through the flow.

Here is one way to tackle a simple task in Node-red using small flows to populate context variables.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/0/a0812719f45379dad9cc7939b7b208d7f68584ec.png)

And an alternate way attaching calculated data to the message as it travels through the flow:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/0/60ea75dacea37036d79b23628182fc4844be82f1.png)

Of course you sometimes want to combine data from multiple inputs and context variables can help here, but again it's generally better if possible to literally combine the messages with a join node.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b237d09a81a154fb1c1ba1c88d2d9f563a96a38.png)

Sorry if that's not a good explanation!

---

<div class="post-metadata">

**Author:** ![iznogoud320](https://avatars.discourse-cdn.com/v4/letter/i/5f9b8f/32.png) [@iznogoud320](https://discourse.nodered.org/u/iznogoud320)\
**Post date:** [9 January 2024 16:53 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/12 "2024-01-09T16:53:13Z")

</div>

> [@jbudd](#):
>
> Sorry if that's not a good explanation!

It's always interesting thanks 😉

To illustrate, I have a flow handling my solar system and providing a calculated value about import/export of power from/to the network  
I also have another flow managing data from my water heater and it's attached solar router.  
Initially I included the logic nodes driving my solar router directly within the heater flow.

But now I want to create a new flow, dedicated to managing loads, that will combine data available from different flows to control many loads, like water heater, EV charger etc. That's why some vars that were local to water heater flow for example must now be accessible from another flow.

I thought about implementing a solution using message flow. linkIn+function node (pulling local context var and returning that to payload )+linkOut. From another flow I can call this with a link call, pass the var name in the topic and get my value in the msg.payload.

```auto
msg.payload=flow.get(msg.topic);
return msg;

```

But I wonder it this is appropriate as it seems to be a kind of workaround of the local/global structure of the variables. Pro is that it's probably faster and simpler to implement than refactoring all my code manually (and risking to miss an instance of my variable somewhere), cons is that it adds another mechanism to get a var through flows, and such a trick has to be copied in each flow where I have to pull some data.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [11 January 2024 01:08 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/13 "2024-01-11T01:08:38Z")

</div>

Yeah i do a horses for courses approach on this - if i am calculating information and then using the calculated result in another flow (which may operate on a different timeframe/basis) then i will usually use Global variables. I also put some thought into horizontal scaling of my system - either through sheer ease of use or for performance reasons.

If i left a single VM running all my stuff i would have approx 80 flows which i find is too much to manage (from my brain perspective) - i prefer to hive flows off to other Virtual machines when i get about 30 or so - i then try and group flows that make sense on the same VM rather than having everything communicating through MQTT etc.

So i have a VM which is dedicated to all the energy stuff in the house - solar, batteries, power monitoring etc.

I have another one that is dedicated to all the automation stuff - lights, media, swimming pool etc etc

I do pass a lot of information as objects within messages also.

I sometimes wonder what the overhead of large message objects are as compared to accessing an Object/array in Global memory.

Either way as i run my NR under a VM i am not fussed as i can always throw more hardware at it.

Craig

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 March 2024 01:09 UTC](https://discourse.nodered.org/t/nodered-refactoring-code-and-autocompletion/84427/14 "2024-03-11T01:09:12Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
