# Store data in JSON file alongside settings.js & include with require

**URL:** <https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852>\
**Category:** General\
**Created:** [8 July 2022 16:50 UTC](https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852 "2022-07-08T16:50:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://discourse.nodered.org/u/awneil)\
**Post date:** [8 July 2022 16:50 UTC](https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852/1 "2022-07-08T16:50:34Z")

</div>

Ref this post (in a thread now locked):

> [@Allow the user to change his/her password for dashboard UI](https://discourse.nodered.org/t/allow-the-user-to-change-his-her-password-for-dashboard-ui/22214/11):
>
> I would store that data in a JSON file alongside your settings.js and include it with require. You can easily generate valid JSON strings using the json node or JSON.stringify() from within a function node. In your settings.js file, using a try/catch around it would be wise, so you can handle errors like invalid format or missing file. Use default values in that case, so Node-RED can start properly. You'd have to restart your Node-RED process, of course. Oh, and don't forget to create the pro…

So how, exactly, would one go about "_including a JSON file in settings.js using require_" ?

@kuema

---

<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:** [8 July 2022 19:23 UTC](https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852/2 "2022-07-08T19:23:02Z")

</div>

In Node.js, you can simply do something like `const mydata = require('/path/to/myfile.json')`

As long as it is valid json, the file will be parsed as a JavaScript object in `mydata`.

For Node-RED, in settings.js, you can use the globals property and add

```auto
    functionGlobalContext: {
        mydata: require('/path/to/mydata.json'),
    },

```

You won't be able to do a try/catch there so you would need to be sure that your JSON file is always valid. For additional safety, load the json before the exported object as in the first example but wrapped:

```auto
let mydata
try {
    mydata = require('/path/to/myfile.json')
} catch (err) {
    console.log('FAILED TO LOAD MY JSON: ', err.message)
}

```

and then assign the mydata variable to the globals.

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://discourse.nodered.org/u/awneil)\
**Post date:** [11 July 2022 16:05 UTC](https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852/3 "2022-07-11T16:05:55Z")

</div>

Thanks.

So specifically how would I provide the `httpNodeAuth` to settings.js from an external JSON file?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [11 July 2022 16:15 UTC](https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852/4 "2022-07-11T16:15:58Z")

</div>

> [@awneil](#):
>
> So specifically how would I provide the `httpNodeAuth` to settings.js from an external JSON file?

take a leap of faith 😉

  

Your `settings.js` file...

```auto
let authSettings
try {
    authSettings = require('myauth.json')
} catch (err) {
    console.log('FAILED TO LOAD MY JSON: ', err.message)
}

module.exports = {
  //...
  //snipped for brevity
  //...

  httpNodeAuth: authSettings,

  //...
  //snipped for brevity
  //...
}

```

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://discourse.nodered.org/u/awneil)\
**Post date:** [13 July 2022 15:18 UTC](https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852/5 "2022-07-13T15:18:22Z")

</div>

> [@Steve-Mcl](#):
>
> take a leap of faith

Hmmm ... all these no/low-code frameworks seem to be a bit of a leap of faith: you hope that you can just get on and do stuff without having to delve into their arcane inner working; but then ...

Anyhow, thanks to you & @TotallyInformation, I got there in the end.

For some reason, the `require` didn't seem to like a path of the form `~/.node-red/myauth.json`, but was OK with `/home/pi/.node-red/myauth.json`

The form from the default `settings.js` file:

```auto
httpNodeAuth: {user:"user",pass:"$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN."},

```

needed extra double-quotes in the `.json` file:

```auto
{
   "user":"user",
   "pass":"$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN."
}

```

I was helped by this JSON formatter/verifier: [https://jsonformatter.curiousconcept.com/#](https://jsonformatter.curiousconcept.com/#)

I guess this is a difference between JSON (file/data format) and JavaScript (language)?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 July 2022 15:26 UTC](https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852/6 "2022-07-13T15:26:40Z")

</div>

> [@awneil](#):
>
> Hmmm ... all these no/low-code frameworks seem to be a bit of a leap of faith: you hope that you can just get on and do stuff without having to delve into their arcane inner working

To be fair - what I meant was - Julian had given you the answer - I really only changed names etc to sound more "auth" related 😉 😃

But also, to give you a gentle push in the direction of "give it a go, but pop back if you get stuck"

The truth is, what you are asking to do was a bit more "behind the curtain stuff" not actually "using low-code" so it was always gonna be a bit sticky.

The hope was (always is) we impart some clues for the OP to solve the issue themselves, they feedback what they did / how theye did it & we all gain (knowledge, time and a solution) for all the efforts we put into these things (and all for free 🙂 )

> [@awneil](#):
>
> I guess this is a difference between JSON (file/data format) and JavaScript (language)?

Yes, JSON must have `"`qoutes`"` (part of its design spec) - fortunately you used an online JSON validator 👍

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [13 July 2022 15:53 UTC](https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852/7 "2022-07-13T15:53:09Z")

</div>

> [@awneil](#):
>
> For some reason, the `require` didn't seem to like a path of the form `~/.node-red/myauth.json`, but was OK with `/home/pi/.node-red/myauth.json`

it will depend on what user Node-RED is running as as to what ~ means... so when running as a service it may well be a different environment than a command line user, that's nothing to do with low-code just the system environment. So yes using a full path is always safer - the same will be true of file in and file out nodes etc.

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://discourse.nodered.org/u/awneil)\
**Post date:** [13 July 2022 16:26 UTC](https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852/8 "2022-07-13T16:26:27Z")

</div>

> [@Steve-Mcl](#):
>
> what I meant was...

Yes, I know!

Thanks for the help.

---

<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:** [13 July 2022 17:57 UTC](https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852/9 "2022-07-13T17:57:38Z")

</div>

> [@awneil](#):
>
> all these no/low-code frameworks seem to be a bit of a leap of faith: you hope that you can just get on and do stuff without having to delve into their arcane inner working; but then ...

That is true of all frameworks of all kinds. The trick is working out where the cliff-edge is and whether you are likely to fall off! Thankfully, Node-RED is built over Node.js which is very well known with a ton of other help and support around. Also around ExpressJS which is the most popular web service for node.js.

Low-code doesn't mean "no" code of course so being able and willing to get coding is a handy skill. Node-RED continues to help even when you need to code because it provides such a great framework.

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://discourse.nodered.org/u/awneil)\
**Post date:** [14 July 2022 16:20 UTC](https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852/10 "2022-07-14T16:20:01Z")

</div>

> [@TotallyInformation](#):
>
> That is true of all frameworks of all kinds

Indeed.

> [@TotallyInformation](#):
>
> The trick is working out where the cliff-edge is and whether you are likely to fall off!

Very true.  
Unfortunately, the cliff edge is often not discovered until after a long walk!

> [@TotallyInformation](#):
>
> Node-RED is built over Node.js which is very well known with a ton of other help and support around.

Indeed.  
The trouble for a newbie is applying all that other general stuff to the specific context of Node-RED, its terminology etc.

---

<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:** [28 July 2022 16:20 UTC](https://discourse.nodered.org/t/store-data-in-json-file-alongside-settings-js-include-with-require/64852/11 "2022-07-28T16:20:52Z")

</div>

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