Read settings.js within custom node

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

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.

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

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

Hello Julian,

Thank you, I found the section:
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

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

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

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:

  // ...
  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.

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

Regards
Lukas

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