# Node red variable definition in version 3.0, what's changed?

**URL:** <https://discourse.nodered.org/t/node-red-variable-definition-in-version-3-0-whats-changed/76921>\
**Category:** General\
**Created:** [25 March 2023 09:37 UTC](https://discourse.nodered.org/t/node-red-variable-definition-in-version-3-0-whats-changed/76921 "2023-03-25T09:37:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mizar62](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mizar62/32/75345_2.png) [@mizar62](https://discourse.nodered.org/u/mizar62)\
**Post date:** [25 March 2023 09:37 UTC](https://discourse.nodered.org/t/node-red-variable-definition-in-version-3-0-whats-changed/76921/1 "2023-03-25T09:37:57Z")

</div>

Hi,

I'm running node red v. 3.0.2.  
I have problems to define and initialize variables in the functions "on start" pane and then using them in the "on message" pane.  
The editor complains that it "Cannot find name 'xxx'" even when 'xxx' is defined in "on start".

In earlier versions of node red, both in vesions 1 and 2 this worked fine.  
So what is changed in version 3, and how to do it?

I want to define and initialize a variable in "on start" and use it in "on message".

Below is a small test function node that illustrates the problem. When running this code, I get error message:"ReferenceError: test\_OnStart is not defined (line 1, col 15)"  
[  
{  
"id": "368473e65280b6f8",  
"type": "function",  
"z": "b451e0f43b8be752",  
"name": "function 3",  
"func": "msg.payload = test\_OnStart;\nreturn msg;",  
"outputs": 1,  
"noerr": 1,  
"initialize": "// Code added here will be run once\n// whenever the node is started.\nvar test\_OnStart = "defined"",  
"finalize": "",  
"libs": ,  
"x": 800,  
"y": 240,  
"wires": [  
[  
"e2fc02ea587ca4a2"  
]  
]  
}  
]

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [25 March 2023 10:15 UTC](https://discourse.nodered.org/t/node-red-variable-definition-in-version-3-0-whats-changed/76921/2 "2023-03-25T10:15:32Z")

</div>

When sharing flows on the forum please take care to format them properly. Use the `</>` button in the toolbar to add a code block.

Your function is:

```auto
msg.payload = test_OnStart;
return msg;

```

But you haven't defined `test_OnStart` anywhere - hence the error.

Node-RED applies JavaScript strict mode to the code which ensures errors like this don't get ignored.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 March 2023 10:43 UTC](https://discourse.nodered.org/t/node-red-variable-definition-in-version-3-0-whats-changed/76921/3 "2023-03-25T10:43:21Z")

</div>

> [@mizar62](#):
>
> I want to define and initialize a variable in "on start" and use it in "on message"

I'm almost certain this is always been the case since on start was introduced.

If you want a variable that was created in on start, you need to store it in context then retrieve it in on message.

---

<div class="post-metadata">

**Author:** ![mizar62](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mizar62/32/75345_2.png) [@mizar62](https://discourse.nodered.org/u/mizar62)\
**Post date:** [25 March 2023 10:43 UTC](https://discourse.nodered.org/t/node-red-variable-definition-in-version-3-0-whats-changed/76921/4 "2023-03-25T10:43:27Z")

</div>

That is exactly my point:

The test\_OnStart variable is defined (and initialized) under the "On Start" pane of the function like this:  
var test\_OnStart = "defined"

So why does the code in "On Message" not have access to the code in "On Start" ?

Best regards,

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 March 2023 10:44 UTC](https://discourse.nodered.org/t/node-red-variable-definition-in-version-3-0-whats-changed/76921/5 "2023-03-25T10:44:39Z")

</div>

> [@mizar62](#):
>
> So why does the code in "On Message" not have access to the code in "On Start"

As mentioned before, it has always been this way. On start and on message are separate VMs under the hood. You need to use context if you want to transfer the variables to on message.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [25 March 2023 10:55 UTC](https://discourse.nodered.org/t/node-red-variable-definition-in-version-3-0-whats-changed/76921/6 "2023-03-25T10:55:14Z")

</div>

As Steve says, it has always been the case that they are run in different scopes.

If you want to store/initialise any values, use Context to store them.

---

<div class="post-metadata">

**Author:** ![mizar62](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mizar62/32/75345_2.png) [@mizar62](https://discourse.nodered.org/u/mizar62)\
**Post date:** [25 March 2023 11:58 UTC](https://discourse.nodered.org/t/node-red-variable-definition-in-version-3-0-whats-changed/76921/7 "2023-03-25T11:58:38Z")

</div>

Fair enough that you must use context in version 3 of node-red.

But i don't agree this has always been like this.  
As an example, I made a quick test example in node red v1.1.3:

I have a inject node, then a function node setting two variables in the "Setup" pane.  
This sets the two variables:  
test\_nr113 = "init\_OK"  
topic = "node-redv1.1.3"

Then I use these variables in the "Function" pane:  
msg.payload = test\_nr113;  
msg.topic = topic;  
return msg;

This works fine without any errors. Debug node prints out the correct payload and topic values.

If I do the same thing in node red v3, I will get errors on undefined variables.

See also attached snapshots.  
[flows (2).json](https://discourse.nodered.org/uploads/short-url/sVbADxegQVg6VfJVrnoxfDzJPY2.json) (1.4 KB)  
 ![nodes](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/af4c812b73d7f6c4bfc89d780d3c940a93e86b8f.jpeg)  
 ![setup](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6ad54f1208c7ba8d8b1ccd36a38cecb6ecfadf94.jpeg)  
 ![function](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/1/015658f4fdda2e96bcf7c2e138ca784d7775ccf1.jpeg)  
 ![debug](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/13f4be522b4dc0cf160b40bf0047843776900458.jpeg)

---

<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:** [24 May 2023 11:59 UTC](https://discourse.nodered.org/t/node-red-variable-definition-in-version-3-0-whats-changed/76921/8 "2023-05-24T11:59:03Z")

</div>

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