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

Ref this post (in a thread now locked):

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

@kuema

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

    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:

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.

1 Like

Thanks.

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

take a leap of faith :wink:


Your settings.js file...

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
  //...
}
2 Likes

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:

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

needed extra double-quotes in the .json file:

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

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

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

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

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 :slight_smile: )

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

1 Like

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.

1 Like

Yes, I know!

Thanks for the help.

1 Like

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.

2 Likes

Indeed.

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

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

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