# Trouble getting Persistent Storage to work

**URL:** https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639
**Category:** General
**Tags:** docker
**Created:** [26 July 2022 10:37 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639 "2022-07-26T10:37:44Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![SSHIntoMe](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@SSHIntoMe](https://discourse.nodered.org/u/SSHIntoMe)
#### Post date: [26 July 2022 10:37 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/1 "2022-07-26T10:37:45Z")

</div>

When attempting to store variables, they simply get lost upon reboot.

This is my **settings.js** :

> module.exports = {  
> flowFile: **'flows.json'** ,  
> flowFilePretty: true,  
> contextStorage: {  
> default: "memoryOnly",  
> memoryOnly: { module: 'memory' },  
> file: { module: 'localfilesystem' }  
> },

**Startup Log:**

> 26 Jul 10:13:23 - [info]
> 
> Welcome to Node-RED
> 
> ===================
> 
> 26 Jul 10:13:23 - [info] Node-RED version: v2.2.2
> 
> 26 Jul 10:13:23 - [info] Node.js version: v14.18.2
> 
> 26 Jul 10:13:23 - [info] Linux 4.15.0 x64 LE
> 
> 26 Jul 10:13:24 - [info] Loading palette nodes
> 
> 26 Jul 10:13:27 - [info] Dashboard version 3.1.6 started at /ui
> 
> 26 Jul 10:13:28 - [warn] rpi-gpio : Raspberry Pi specific node set inactive
> 
> 26 Jul 10:13:28 - [info] Settings file : /data/settings.js
> 
> 26 Jul 10:13:28 - [info] Context store : 'memoryOnly' [module=memory]
> 
> 26 Jul 10:13:28 - [info] Context store : 'file' [module=localfilesystem]
> 
> 26 Jul 10:13:28 - [info] User directory : /data
> 
> 26 Jul 10:13:28 - [info] Projects directory: /data/projects
> 
> 26 Jul 10:13:28 - [info] Server now running at [https://127.0.0.1:1880/](https://127.0.0.1:1880/)
> 
> 26 Jul 10:13:28 - [info] Active project : Test2
> 
> 26 Jul 10:13:28 - [info] Flows file : /data/projects/tests2/flows.json
> 
> 26 Jul 10:13:28 - [info] Starting flows
> 
> 26 Jul 10:13:28 - [info] Started flows
> 
> 26 Jul 10:13:28 - [info] [mqtt-broker:randomID] Connected to broker: mqtt://my.secret.IP:1883

To test, I set the variable, restart nodered, then try to retrieve the value.

**Set the value:**

> flow.set("ID", 123, "file");  
> msg.payload = flow.get("ID", "file");  
> return msg;

**Retrieve the value:**

> var count = flow.get("ID", "file")||0;  
> msg.payload = count;  
> return msg;

**Expected result:**  
After a restart, the msg.payload value should be "123".

**Actual result:**  
After a restart, the msg.payload value is "0".

In addition, the change node doesn't give an option to select contextStorage, as if there's only one option available.

Does anyone have an idea, why this doesn't work.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [26 July 2022 11:01 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/2 "2022-07-26T11:01:16Z")

</div>

Your code is working for me. What version of NR are you running?  
When you run the 'add' flow, what do you see if you go to the 'Context data' option in the sidebar and click the flow refresh?  
 ![context](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/495fa607ed825c4de0895b006995eab6e0809f74.jpeg)  
And what do you see after a stop/start of NR?

I would suggest you change your settings file to use:

```auto
			default : { module: "localfilesystem" },
 			memoryOnly : { module: "memory" }

```

Then it will always go to the file unless you specify 'memoryOnly'

---

<div class="post-metadata">

### Author: ![SSHIntoMe](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@SSHIntoMe](https://discourse.nodered.org/u/SSHIntoMe)
#### Post date: [26 July 2022 11:25 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/3 "2022-07-26T11:25:07Z")

</div>

I'm using Nodered v2.2.2

This is how the 'Context data' looks after setting the variable:  
 ![Screenshot 2022-07-26 at 13.19.41](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/75a000f20ab1284119a4f48d930f9fff086dc442.png)

And this is how the 'Context data' looks after restarting the flow and after retrieving the variable:  
 ![Screenshot 2022-07-26 at 13.20.31](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/98d8238b97e2f2edf76ea4363e4133f6a3410d6f.png)

**Edit** :  
I installed NR following the NR docker install tutorial. Might that pose any problems?

---

<div class="post-metadata">

### Author: ![iRob](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/irob/32/65242_2.png) [@iRob](https://discourse.nodered.org/u/iRob)
#### Post date: [26 July 2022 11:41 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/4 "2022-07-26T11:41:58Z")

</div>

Have you checked following ?

- it appears you are using default settings which uses dir=~/.node-red and base=context -\> can you see if node-red produces files in that directory -\> ~/.node-red/context/ ?

- is the directory writeable for node-red daemon user?

- if you are using docker - is the direcotry on a persistent filesystem (bind-mount or volume) ?

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [26 July 2022 14:50 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/5 "2022-07-26T14:50:14Z")

</div>

> [@SSHIntoMe](#):
>
> I installed NR following the NR docker install tutorial. Might that pose any problems?

It might, you might want to ask on a docker forum. I've never used docker.

Have you tried what @iRob suggested?

---

<div class="post-metadata">

### Author: ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)
#### Post date: [26 July 2022 15:17 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/6 "2022-07-26T15:17:23Z")

</div>

I set all my Node-RED `setting.js` up like this for context:

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

```

basically like [zenofmud](https://discourse.nodered.org/u/zenofmud) suggested.

This should default to saving context as a `file` rather than in memory, unless you specify that you want `memoryOnly` when setting the variable.

---

<div class="post-metadata">

### Author: ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)
#### Post date: [26 July 2022 15:20 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/7 "2022-07-26T15:20:37Z")

</div>

Scratch that, I see you are already specifying `file`. Perhaps this is issue with Docker not being able to create the context file due to permissions or something like that?

---

<div class="post-metadata">

### Author: ![SSHIntoMe](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@SSHIntoMe](https://discourse.nodered.org/u/SSHIntoMe)
#### Post date: [26 July 2022 15:52 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/8 "2022-07-26T15:52:12Z")

</div>

Ok, i changed the default directory and now the variables persist through a restart. The default dir must have been inaccessible.

However, there's a new issue: When changing the project and then reopening the old project, the variables get lost, but as long as I only use one project, I can restart without loosing data.

---

<div class="post-metadata">

### Author: ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)
#### Post date: [26 July 2022 15:55 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/9 "2022-07-26T15:55:19Z")

</div>

~~I believe context storage is separate for each project when using `file`, because they are stored in a separate file in each projects folder.~~

---

<div class="post-metadata">

### Author: ![LosinIt](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@LosinIt](https://discourse.nodered.org/u/LosinIt)
#### Post date: [26 July 2022 16:08 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/10 "2022-07-26T16:08:56Z")

</div>

In Docker all path/file references are INSIDE the container, not to the host's filesystem. To enable using the host's filesystem you need to add "-v /path-to-desired-host-directory:/data" to your docker command that starts NR. You may also need to add privileges with "--group-add whatever-group" which can be repeated as many times as needed. More information on [docker](https://nodered.org/docs/getting-started/docker)

---

<div class="post-metadata">

### Author: ![SSHIntoMe](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@SSHIntoMe](https://discourse.nodered.org/u/SSHIntoMe)
#### Post date: [26 July 2022 16:48 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/11 "2022-07-26T16:48:05Z")

</div>

Just to clarify, these are the steps:

- use flow.set("ID", 123, file) in project1
- open project2
- do nothing
- open project1
- run flow.get("ID, "file")||0  
and it returns 0.

I'm not expecting the variable to be available in another project, just that it's still there, when I return to the project.

---

<div class="post-metadata">

### Author: ![SSHIntoMe](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@SSHIntoMe](https://discourse.nodered.org/u/SSHIntoMe)
#### Post date: [26 July 2022 17:01 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/13 "2022-07-26T17:01:19Z")

</div>

Sorry, that was a spelling mistake on my part. I did in fact use `flow.set("ID, 123, file)` and have edited the previous post for clarity.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [26 July 2022 17:12 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/14 "2022-07-26T17:12:51Z")

</div>

> [@HaroldPetersInskipp](#):
>
> I believe context storage is separate for each project when using `file`, because they are stored in a separate file in each projects folder.

No this not true. All persistant data is stored in the `.node-red/context` folder. Within that folder is a folder called `global` where global data resides. Flow data is stored seperately in folders where the folder name is the ID of the flow.

So persistant `flow` data is not shared between projects but `global` data is.

---

<div class="post-metadata">

### Author: ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)
#### Post date: [26 July 2022 17:22 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/15 "2022-07-26T17:22:25Z")

</div>

I see, thank you for that clarification.

---

<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 September 2022 17:22 UTC](https://discourse.nodered.org/t/trouble-getting-persistent-storage-to-work/65639/16 "2022-09-24T17:22:30Z")

</div>

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