# How to just change the global variable name once

**URL:** <https://discourse.nodered.org/t/how-to-just-change-the-global-variable-name-once/33027>\
**Category:** General\
**Created:** [18 September 2020 09:17 UTC](https://discourse.nodered.org/t/how-to-just-change-the-global-variable-name-once/33027 "2020-09-18T09:17:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [18 September 2020 09:17 UTC](https://discourse.nodered.org/t/how-to-just-change-the-global-variable-name-once/33027/1 "2020-09-18T09:17:23Z")

</div>

I use the **config** node `node-red-contrib-config` to declare global variables like so:

![node-config-1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d4f06c7c320828a88f73537604b027267240bfc3.png)

And in `function` node I get the value of the global **aaa** variable by `global.get('aaa')`

 ![node-config-2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/8979312cbdc6b7076138b91be7a027ab3e076417.png)

```auto
[{"id":"d1a34c16.5a144","type":"config","z":"aa40b590.ff1888","name":"","properties":[{"p":"aaa","pt":"global","to":"AAA","tot":"str"}],"active":true,"x":1740,"y":3020,"wires":[]},{"id":"f6f15e86.bd382","type":"function","z":"aa40b590.ff1888","name":"","func":"var value = global.get('aaa');\nmsg.payload = value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1740,"y":3080,"wires":[["53fe2cd9.4d1274"]]},{"id":"977a972b.ea5e18","type":"inject","z":"aa40b590.ff1888","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1590,"y":3080,"wires":[["f6f15e86.bd382"]]},{"id":"53fe2cd9.4d1274","type":"debug","z":"aa40b590.ff1888","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1910,"y":3080,"wires":[]}]

```

**The problem I am having is:**  
in case there are multiple `function` nodes using the same global **aaa** variable, and get the value by `global.get('aaa')` as above,  
then if I change the variable name **aaa** in `config` node (suppose to become _bbb_) then I have to go to all `function` nodes to change the variable name **aaa** of `global.get('aaa')` to `global.get('bbb')`

This is inconvenient, is there any way I can use a global variable that when changing the variable name, I only need to change the variable name once?

---

<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:** [18 September 2020 09:43 UTC](https://discourse.nodered.org/t/how-to-just-change-the-global-variable-name-once/33027/2 "2020-09-18T09:43:26Z")

</div>

> [@minhhn0205](#):
>
> node-red-contrib-config

Sorry, but I must be missing something because I can't really see the need for that node since a change node (which is a core node) will do the job anyway.

> [@minhhn0205](#):
>
> This is inconvenient, is there any way I can use a global variable that when changing the variable name, I only need to change the variable name once?

Well, you could use another global variable to store the variable name 🕶

But realistically, it is easier not to change the name once you've used it. If you were really desperate, you could shut down Node-RED and use a tool such as AWK or a code editor to do a search and replace in the flow file (having first taken a backup copy of course).

It is for this reason that I tend to use longer rather than shorter variable names (e.g. 10-20 chars is generally a lot better than 3-5) and try to make them as descriptive as possible.

---

<div class="post-metadata">

**Author:** ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)\
**Post date:** [18 September 2020 10:06 UTC](https://discourse.nodered.org/t/how-to-just-change-the-global-variable-name-once/33027/3 "2020-09-18T10:06:20Z")

</div>

> [@minhhn0205](#):
>
> **The problem I am having is:**  
> in case there are multiple `function` nodes using the same global **aaa** variable, and get the value by `global.get('aaa')` as above,  
> then if I change the variable name **aaa** in `config` node (suppose to become _bbb_ ) then I have to go to all `function` nodes to change the variable name **aaa** of `global.get('aaa')` to `global.get('bbb')`

Could you maybe explain what you're trying to do by changing the variable name please? I can't really think of a use-case for this, and it seems a bit like renaming a variable in your function code :

> var value = global.get('aaa');  
> ...  
> rename value = newvalue;  
> ...  
> msg.payload = newvalue;  
> return msg;

What is the reason for wanting to rename the global variable? As @TotallyInformation says, it's a good idea to give all variables, whether environment ones or in function code, meaningful names which describe their purpose and don't conflict with others.

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [18 September 2020 11:12 UTC](https://discourse.nodered.org/t/how-to-just-change-the-global-variable-name-once/33027/4 "2020-09-18T11:12:08Z")

</div>

> [@TotallyInformation](#):
>
> Sorry, but I must be missing something because I can't really see the need for that node since a change node (which is a core node) will do the job anyway.

Thanks for the instruction, since i don't know a `change` node can do the same thing, i will use `change` node.

Even using a `change` node, the thing as I described above cannot change the global variable name once, right?

> [@TotallyInformation](#):
>
> Well, you could use another global variable to store the variable name 🕶

This is really complicated, right 😊

> [@TotallyInformation](#):
>
> But realistically, it is easier not to change the name once you've used it. If you were really desperate, you could shut down Node-RED and use a tool such as AWK or a code editor to do a search and replace in the flow file (having first taken a backup copy of course).

I don't know the AWK tool, can you tell me a little more clearly?

> [@TotallyInformation](#):
>
> It is for this reason that I tend to use longer rather than shorter variable names (e.g. 10-20 chars is generally a lot better than 3-5) and try to make them as descriptive as possible.

Thank you for the advice, but this is just a description to make it easy to understand, I did not actually name this variable ^^

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [18 September 2020 11:14 UTC](https://discourse.nodered.org/t/how-to-just-change-the-global-variable-name-once/33027/5 "2020-09-18T11:14:28Z")

</div>

> [@molesworth](#):
>
> Could you maybe explain what you're trying to do by changing the variable name please? I can't really think of a use-case for this, and it seems a bit like renaming a variable in your function code :

Since the global variable naming was erroneous in the beginning, I wanted to change it...

---

<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:** [18 September 2020 11:27 UTC](https://discourse.nodered.org/t/how-to-just-change-the-global-variable-name-once/33027/6 "2020-09-18T11:27:05Z")

</div>

If the name you used is a unique string then you might get away with renaming every occurrence of that string in the flows file. Make a copy of it first (it is in your .node-red directory will probably be called `flows_<server_name>.json`) in case of disasters. Stop node-red, use your favourite text editor or command line s/w (that is what awk was referring to) and replace all occurrences with the new name. Check them as it does them to make sure it has not obviously picked up an instance that you did not intend. Then restart node-red and see if all is well. If you don't understand then you would be quicker just to change them all manually. You can use the Search feature in the node-red editor to find them.

---

<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:** [18 September 2020 16:45 UTC](https://discourse.nodered.org/t/how-to-just-change-the-global-variable-name-once/33027/7 "2020-09-18T16:45:48Z")

</div>

> [@minhhn0205](#):
>
> This is really complicated, right

Yes, a jest, not a real suggestion.

> [@minhhn0205](#):
>
> AWK

awk is a Linux/UNIX command line tool. If you don't already know it, forget it and use a text editor. But honestly, if you aren't sure what you are doing with the flow file, leave it alone. You will certainly break it from time to time if you do this.

> [@minhhn0205](#):
>
> but this is just a description to make it easy to understand, I did not actually name this variable ^^

Yes, I understood that but the advice remains - give variables a good name to start with and don't mess with them. If, occasionally, you have to anyway, accept the pain as a lesson to name things better for next time.

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [18 September 2020 20:45 UTC](https://discourse.nodered.org/t/how-to-just-change-the-global-variable-name-once/33027/8 "2020-09-18T20:45:26Z")

</div>

Thank everyone very much!

---

<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:** [2 October 2020 20:45 UTC](https://discourse.nodered.org/t/how-to-just-change-the-global-variable-name-once/33027/9 "2020-10-02T20:45:56Z")

</div>

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