# Nodered/uibuilder reading json files

**URL:** https://discourse.nodered.org/t/nodered-uibuilder-reading-json-files/76325
**Category:** General
**Tags:** uibuilder
**Created:** [8 March 2023 23:48 UTC](https://discourse.nodered.org/t/nodered-uibuilder-reading-json-files/76325 "2023-03-08T23:48:09Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)
#### Post date: [8 March 2023 23:48 UTC](https://discourse.nodered.org/t/nodered-uibuilder-reading-json-files/76325/1 "2023-03-08T23:48:09Z")

</div>

uibuilder works well to migrate my older project to NR/uib.  
Problem (atm) is to get/read json file(s) to use that data in index.js.  
(And yes, I need to read dircetly from rpi files!)

Followed @knolleary advice with `/handling-json-in-the-file/7823`.. added  
to `~/.node-red/settings.json`

```
functionGlobalContext: {
    os:require('os'),
    fs:require('fs')
},

```

On the RPI installed 'fs' with `~/.node-red $ npm install fs`

In index.js :

```
var fs = global.get('fs'); <--- this is line #35
const jsonString = fs.readFileSync("./plus/stringsPlus.json");
const stringsPlus = JSON.parse(jsonString);

```

Restarting NR and reloading the NR/uibuilder page, the DeveloperConsole gives:

```auto
    Uncaught ReferenceError: global is not defined
        at index.js:35:10

```

_ **Is this methode not valid for NR/uibuilder?** _  
Maybe I have a config error, but how to debug this?

---

<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 March 2023 23:59 UTC](https://discourse.nodered.org/t/nodered-uibuilder-reading-json-files/76325/2 "2023-03-08T23:59:35Z")

</div>

The `index.js` _runs_ in your users browser, global and fs run in node-red on the server. So no, you can't use global or the fs library in index.js. The global is node-red only - it only works in flows. `fs` is node.js and only works in the node.js context (in function nodes from a node-red perspective).

The real question is: Do you want to read the JSON file and process it in Node-RED and then send some data to your front-end (the browser) or do you want to read the JSON file into the browser?

Reading the file in Node-RED is easier (less manual code to write). Node-RED has the file read and json nodes which between them get the file (from the server's filing system) and converts the JSON to a JavaScript object. You can then send that to your front-end by outputting direct to the uibuilder node. It arrives in the browser as a fully functional JavaScript object.

---

<div class="post-metadata">

### Author: ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)
#### Post date: [9 March 2023 00:04 UTC](https://discourse.nodered.org/t/nodered-uibuilder-reading-json-files/76325/3 "2023-03-09T00:04:21Z")

</div>

> [@TotallyInformation](#):
>
> do you want to read the JSON file into the browser?

afaiu with `index.js` and `index.html` etc I'm reading it into the 'browser'.

---

<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: [9 March 2023 00:07 UTC](https://discourse.nodered.org/t/nodered-uibuilder-reading-json-files/76325/4 "2023-03-09T00:07:55Z")

</div>

OK, but did you see my last bit:

> [@TotallyInformation](#):
>
> Reading the file in Node-RED is easier (less manual code to write). Node-RED has the file read and json nodes which between them get the file (from the server's filing system) and converts the JSON to a JavaScript object. You can then send that to your front-end by outputting direct to the uibuilder node. It arrives in the browser as a fully functional JavaScript object.

You can read the file in Node-RED and send the _data_ to the browser. Or you can indeed read the file straight into the front end - but only by making it available as a static file that the browser has to effectively download from the web server (using something like the `fetch` function in JavaScript.

Maybe this diagram will help you understand the different contexts that Node-RED and the browser are running in?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c2cb2430e3f54a1878452ceec03ca9b593c20bdd.jpeg)

To read the JSON direct into the index.js - you must first put it in the server filing system. Then make it available to the web server (most commonly by putting it into the same folder as the index.js file). Then you must `fetch` it from the web server.

To send the JSON _data_ to index.js - you first read it into Node-RED from the server filing system as a plain file. Then run it through the JSON node to turn the text into data. Then send it into the uibuilder node. Finally, you can handle it in the front-end via a `uibuilder.onChange('msg', (msg) =\> { ... }) function.

---

<div class="post-metadata">

### Author: ![gNeandr](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gneandr/32/73947_2.png) [@gNeandr](https://discourse.nodered.org/u/gNeandr)
#### Post date: [9 March 2023 00:11 UTC](https://discourse.nodered.org/t/nodered-uibuilder-reading-json-files/76325/5 "2023-03-09T00:11:04Z")

</div>

Seems I should rethink the approach ... good starting point for tomorrow 😉  
_Thanks for the moment._

---

<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: [8 May 2023 00:11 UTC](https://discourse.nodered.org/t/nodered-uibuilder-reading-json-files/76325/6 "2023-05-08T00:11:56Z")

</div>

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