# How to get get all global variables?

**URL:** https://discourse.nodered.org/t/how-to-get-get-all-global-variables/42005
**Category:** General
**Created:** [4 March 2021 12:21 UTC](https://discourse.nodered.org/t/how-to-get-get-all-global-variables/42005 "2021-03-04T12:21:30Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sathishkumarc14](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@sathishkumarc14](https://discourse.nodered.org/u/sathishkumarc14)
#### Post date: [4 March 2021 12:21 UTC](https://discourse.nodered.org/t/how-to-get-get-all-global-variables/42005/1 "2021-03-04T12:21:30Z")

</div>

I need to get all global context variables. Is there any function to get all global variable?

---

<div class="post-metadata">

### Author: ![henrycosta05](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henrycosta05/32/36350_2.png) [@henrycosta05](https://discourse.nodered.org/u/henrycosta05)
#### Post date: [4 March 2021 19:04 UTC](https://discourse.nodered.org/t/how-to-get-get-all-global-variables/42005/2 "2021-03-04T19:04:26Z")

</div>

hi @sathishkumarc14,  
have you tried change node?

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

OR

using function node  
[https://nodered.org/docs/user-guide/writing-functions#global](https://nodered.org/docs/user-guide/writing-functions#global)

something like below piece of code.  
var xpto = global.get("xpto");  
var xpto2 = global.get("xpto2");

---

<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: [4 March 2021 20:09 UTC](https://discourse.nodered.org/t/how-to-get-get-all-global-variables/42005/3 "2021-03-04T20:09:04Z")

</div>

There is a setting in the settings.js file that exposes the top-level of global and so lets you get a catalogue of all of the variables. Sorry, the details escape me right now but it is in the docs I'm sure.

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [4 March 2021 23:53 UTC](https://discourse.nodered.org/t/how-to-get-get-all-global-variables/42005/4 "2021-03-04T23:53:40Z")

</div>

Good question. I am not aware of a way to get all the context with a single function.

As @TotallyInformation mentioned you can do a change in `settings.js` , more specifically setting the property `exportGlobalContextKeys` to true. This will allow you to get a list of keys with the function `global.keys()`. Such list may be used to programmatically retrieve all the context. Another alternative is to read the files that hold the context persisted in the filesystem. In a raspberry pi you can find these files in the folder `/home/pi/.node-red/context/`

Code example:

```auto
let keys = global.keys();

for (let k of keys) {
    node.warn("Key = " + k);
    node.warn(global.get(k));
}

return msg;

```

---

<div class="post-metadata">

### Author: ![sathishkumarc14](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@sathishkumarc14](https://discourse.nodered.org/u/sathishkumarc14)
#### Post date: [5 March 2021 04:24 UTC](https://discourse.nodered.org/t/how-to-get-get-all-global-variables/42005/5 "2021-03-05T04:24:14Z")

</div>

Thank you all for great information.

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [5 March 2021 18:14 UTC](https://discourse.nodered.org/t/how-to-get-get-all-global-variables/42005/6 "2021-03-05T18:14:50Z")

</div>

I forgot that it is possible to [get multiples values in a single get statement.](https://nodered.org/docs/user-guide/writing-functions) which results that it is possible to use a single statement in a function node to get all the global context variables.

```auto
msg.payload = global.get(global.keys());

```

The result is an array with each and every object stored in the global context.

---

<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: [19 March 2021 18:14 UTC](https://discourse.nodered.org/t/how-to-get-get-all-global-variables/42005/7 "2021-03-19T18:14:57Z")

</div>

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