Where to find settings.js for storing contect in a file

I do like to store my Global vars into a file beside memory.

I did find this site:
https://nodered.org/docs/user-guide/context

It says you need to set up a settings.js file with:

contextStorage: {
…default: {
…….module: “localfilesystem”
…}
},

I did find this file in /config/node-red
I did add the above.
Did store an Global var with node-RED.
Shutdown HA (after more then a minute, it is refershing the file every 30 minutes)

After restart the var was gone.
What did I do wrong?

Knipsel

If I look beside global.vuilnisbak the remove “X” is a little bit moved to the left expecting to see something beside the X??
What to do?

It is working.
I think I was still to quick to shutdown the RPI and the vars where not stored at file.

Dear Canedje and All setting.js experts,

added the following to my setting.js within folder /config/node-red:

contextStorage: {
store: {
module:“localfilesystem”
},
default: {
module:“memory”
}
},

Restarted the RPI and can’t still find the option to store global varaibles to “file” within a change note

looking forward to your hint /quidance.

Tx M

Deleting Browser Data solved it !
Tx M

Whenever I try to set the contextstorage node red won’t boot. It throws an error:

[09:32:52] INFO: Starting Node-RED...
> start
> node $NODE_OPTIONS node_modules/node-red/red.js "--settings" "/etc/node-red/config.js"
Error loading settings file: /etc/node-red/config.js
/config/node-red/settings.js:46
   default: "file",
   ^^^^^^^
SyntaxError: Unexpected token 'default'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/etc/node-red/config.js:1:16)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)

What I did to try to configure the storage:
Opened the file \192.168.1.5\config\node-red\settings.js
I’ve tried to add somewhere early on in the file either of these two snippets of code:

contextStorage: {
   default: "file",
   memoryOnly: { module: 'memory' },
   file: { module: 'localfilesystem' }
},
contextStorage: {
   default: {
       module: "localfilesystem"
   }
},

Does it matter where I add it in the file? Or am I doing something else wrong?

What’s even more strange is that I initially added this functionality a few days ago using the below code:

contextStorage: {
   default: {
       module: "localfilesystem"
   }
}

Note that there’s no comma at the end. This was working but then suddenly I got the error. Not sure why.

I believe it is there already, contextStorage, in settings.js by default. Check to make sure it isn’t there twice. I also have it with the comma.

Thanks a lot @Mikefila,

I carefully compared your screenshot with my code and noticed that in my code I use 3 spaces rather than 4. This solved the issue :wink:

To hijack a thread or start a new one…

Since my question is so similar, I decided to just continue here.

I added this to my settings.js:

	contextStorage: {
       default: {
           module: "localfilesystem"
       }
    }

Where is the file containing the context data stored? According to the Node Red documents it would be in ~/.node-red/context/, but in the Home Assistant Node Red Addon, there is no .node-red folder.

I am facing the same issue. I need to restore my global variables after a restart, but I cannot locate the settings.js file in my HO filesystem. I am using a function where I store values like this:
global.set('name', value)
How can I find the settings file for Node-Red or store global variables to a file?

Yes probably time for an update on this!

The HA folders have recently moved things around to new locations. Add-on configurations are now held in /addon_configs, and within this Node-RED now has its own folder ‘a0d7b954_nodered’, and within that you will find the Node-RED settings.js file

(at least, that is where I now find mine!)

I have Samba share set up on my HA, which is very convenient for setting up network locations on my PC, so I can get to the various key folders directly.

The ‘context store’ bit is towards the bottom on the settings file. I just set up an extra ‘persist’ context using the ‘localfilesystem’ which works nicely for me. Note that the file system context store apparently only flushes to disk every 30 minutes, so an unexpected shutdown may still result in data loss.

With more than one context store available, the storeName should be used to define which one in the function call (I think this is optional and defaults to the first/default store if not set).

See “Storing data - multiple context stores” in https://nodered.org/docs/user-guide/writing-functions

// Set value - sync
flow.set("count", 123, storeName);

Another tip - when using the change node to read back out of context (eg into msg.payload) use the ‘deep copy’ option. This forces the node to make a new copy of the data, which is important when working with objects as they are normally copied by reference, not by value. Copying by reference means that, any change to the object now in msg.payload, will also change the object in context.

Please make sure you take a copy of the original settings file first!