Handling JSON in the file


Hi Guys!
I try to work with JSON file to persist important parameters and I using at first moment, to study the best way to handling it, I starting to read a file, to do it i using REQUIRE Method, and I had this error "require is not defined (line 1, col 10)"

My function GetJSON Parameters:

var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('/home/pi/HomeGateway/nwkFilestorage.json', 'utf8'));
return msg;

My nodes:

[{"id":"8213a1aa.82424","type":"function","z":"36fc56fb.15e79a","name":"GetJSON Parameters","func":"var fs = require('fs');\nvar obj = JSON.parse(fs.readFileSync('/home/pi/HomeGateway/nwkFilestorage.json', 'utf8'));\nmsg.payload=obj;\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":120,"wires":[["136175e2.bd12fa"]]},{"id":"136175e2.bd12fa","type":"debug","z":"36fc56fb.15e79a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":660,"y":120,"wires":[]},{"id":"2a056dd2.f9a382","type":"inject","z":"36fc56fb.15e79a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":90,"wires":[["8213a1aa.82424"]]}]

The error:
I have checked "setting.js" and I didn´t see anything to fix at the first moment.
:slight_smile: 36

My nodes:

My Settings:

Can someone have any idea how to fix it?

Best Regards,


Hi @aargollo

you cannot use require in a Function node.

You have correctly added fs to your functionGlobalContext setting, but you are missing the next piece from https://nodered.org/docs/writing-functions#loading-additional-modules which explains how to then access that module in your code.

var fs = global.get('fs');


But I have to ask why not use the file node to read the file directly ?


Thanks dceejay, I have to use node to ready the file directly. Thanks!
Hi Knolleary, thanks right now is working!!!

I have to use, before storage in the file, check if the device exist through JSON object property called "Nwk_IEEEAddrS" ! To do it I using the code bellow, right now I´m sure that I got success to check if file exist, but I have difficult to find an object parameter in the file.

var fs = global.get('fs');
fs.readFile('/home/pi/HomeGateway/devices.json', 'utf8',function (err, data) {

        /*obj = JSON.parse(data);
        if (err) {
            throw err;
            if (data === ""){

                console.log('data does not exsist');

                console.log('exsist data');



my JSON file - devices.json:

devices.json content file

Nwk_IEEEAddrS  10137a101011d94d
ddrs2":"c3","EP1s":"11","Desc":"Zigbee device actuators, lamps or secrurity things"}]
Nwk_IEEEAddrS  10137a1010109a99
ddrs2":"83","EP1s":"1a","Desc":"Zigbee device actuators, lamps or secrurity things"}]
Nwk_IEEEAddrS  10137a1010109a99
drs2":"83","EP1s":"1a","Desc":"Zigbee device actuators, lamps or secrurity things"}

I Would like to find if Nwk_Addr exist using "Nwk_IEEEAddrS" , ex "10137a1010109a99", but I have doubts how to find it in the file.
Anybody can help me, please?


Hi @aargollo

how is that file being generated? It is not a proper JSON file which will make your task harder than it could be.

If you are not able to change the format of the file to be valid JSON, then you'll have to do something along the lines of:

  1. once you've loaded the file content, split it into individual lines:

     var lines = data.split("\n");
  2. scan through the lines array looking for the address you are interested in. Once you find it, return the next line as that presumable contains the information about the device you are looking for

    var theAddress = "10137a1010109a99";
    for (var i=0;i<lines.length; i++) {
        if (lines[i].indexOf(theAddress) > -1) {
          // Found it, skip to next line.
    if (i < lines.length) {
        msg.payload = JSON.parse(lines[i]);
        return msg;


Hi My friend, thanks a lot for your help!
Right now is working well. I´ll working on to improve my JSON file as you advise me!

Best Regards,


Hi Knolleary,
I working on my JSON file to fix the correct format, as you can see bellow:

Note: I did not implement at this moment the code to prevent duplicated record!