# Read settings.js within custom node

**URL:** https://discourse.nodered.org/t/read-settings-js-within-custom-node/58401
**Category:** Developing Nodes
**Created:** [15 February 2022 15:23 UTC](https://discourse.nodered.org/t/read-settings-js-within-custom-node/58401 "2022-02-15T15:23:25Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![LGaam](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lgaam/32/56524_2.png) [@LGaam](https://discourse.nodered.org/u/LGaam)
#### Post date: [15 February 2022 15:23 UTC](https://discourse.nodered.org/t/read-settings-js-within-custom-node/58401/1 "2022-02-15T15:23:25Z")

</div>

Hello,

We would like to backup the flows.json through mqtt to the broker. The task itself is working perfect.  
For different platforms we would like to read the settings.js to find the correct path to the flows.json.  
Is this possible through global context?

Thank you for the help and regards  
Lukas

---

<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: [15 February 2022 15:55 UTC](https://discourse.nodered.org/t/read-settings-js-within-custom-node/58401/2 "2022-02-15T15:55:09Z")

</div>

Hi @LGaam

in general, nodes don't know anything about how or where the flows are stored. The entire storage layer of node-red is pluggable - for example using a database adapter rather than the default local file system.

Even when using the local file system, you also have the Projects feature that means the flow file is defined in the project.

All of that said, if you're creating this for your own environment and not a general set of nodes for others to use, and you know that

1. its using the local filesystem
2. it isn't using the projects feature
3. you know the flows file has been specified in the settings file (rather than using the default of generating the filename based on hostname)

Then you could add something to the `functionGlobalContext` option in your settings file to expose the flow file name into context as well.

And don't forget that if you want to backup the flow file, you'll also need to back up the credentials file - and ensure you have the credentialSecret saved somewhere safe as you'll need that to decrypt it in the future.

---

<div class="post-metadata">

### Author: ![LGaam](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lgaam/32/56524_2.png) [@LGaam](https://discourse.nodered.org/u/LGaam)
#### Post date: [15 February 2022 16:32 UTC](https://discourse.nodered.org/t/read-settings-js-within-custom-node/58401/3 "2022-02-15T16:32:12Z")

</div>

Good afternoon Nick,  
Just want to say thank again you for your extremely helpful project NodeRED!

About the topic: I added already the path to the functionGlobalContext, but was not successful to add it to our custom node. Could you guid me to this?  
And sure we add the flows\_cred.json as well to the broker DB.

Thank you in advance and regards from Switzerland  
Lukas Gaam

---

<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: [15 February 2022 17:51 UTC](https://discourse.nodered.org/t/read-settings-js-within-custom-node/58401/4 "2022-02-15T17:51:38Z")

</div>

> [@LGaam](#):
>
> functionGlobalContext

That just adds to the global variables. The docs have a section on using global and context vars from within a custom node.

---

<div class="post-metadata">

### Author: ![LGaam](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lgaam/32/56524_2.png) [@LGaam](https://discourse.nodered.org/u/LGaam)
#### Post date: [15 February 2022 19:38 UTC](https://discourse.nodered.org/t/read-settings-js-within-custom-node/58401/5 "2022-02-15T19:38:50Z")

</div>

Hello Julian,

Thank you, I found the section:  
[https://nodered.org/docs/creating-nodes/context](https://nodered.org/docs/creating-nodes/context)

but after trying different versions, still no result.  
Would you mind to quickly write an example?

Thank you and regards  
Lukas

---

<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: [15 February 2022 19:58 UTC](https://discourse.nodered.org/t/read-settings-js-within-custom-node/58401/6 "2022-02-15T19:58:56Z")

</div>

Can you see the entry you expect in the context data sidebar in the Editor?

---

<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: [15 February 2022 20:05 UTC](https://discourse.nodered.org/t/read-settings-js-within-custom-node/58401/7 "2022-02-15T20:05:39Z")

</div>

To be honest, I think you would be better off using something like this in your custom node:

```nohighlight
const path = require('path')

const flowFilePath = path.join( RED.settings.userDir, RED.settings.flowFile )

```

Assuming that you have set the flowFile property. I'm not sure if that contains anything if Node-RED chooses the flow filename itself, I've not tried in ages.

I'm pretty sure that userDir always contains the correct location though whether you set it or not? I always pass it as a startup parameter to Node-RED though so I don't know that I've tested it. If it doesn't work, fall back to doing something like:

```nohighlight
  // ...
  functionGlobalContext: {
    // Even this isn't possibly that good an idea
    // - better to not use global for things like this in a production environment
    _userDir: __dirname,
  },
  // ...

```

in your settings.js file. Then you should see a global variable in the Node-RED editor that contains the userDir (because the settings.js file is also in the userDir folder).

Note that you can't get the flowFile name directly in the functionGlobalContext - you would have to set a variable outside the `export` object and use that variable to set `flowFile` and put in the globals.

---

<div class="post-metadata">

### Author: ![LGaam](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lgaam/32/56524_2.png) [@LGaam](https://discourse.nodered.org/u/LGaam)
#### Post date: [16 February 2022 17:28 UTC](https://discourse.nodered.org/t/read-settings-js-within-custom-node/58401/8 "2022-02-16T17:28:04Z")

</div>

Thank you very much Julian, I will try this and come back.

Regards  
Lukas

---

<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: [17 April 2022 17:28 UTC](https://discourse.nodered.org/t/read-settings-js-within-custom-node/58401/9 "2022-04-17T17:28:41Z")

</div>

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