fs.readFile, fs.writeFile, fs.readdir

Hi all,
I'm writing a node-red function and I would like read and write files inside a function using fs module. I correctly imported it but when I tried to read a file the following message appears:
"TypeError: Cannot read properties of undefined (reading 'readFileSync')" (the same with fs.readFile)

PS. When I use writeFile the same error appears but it create and write file and I don't understand why.

can someone help me?
many thanks

How did you import fs?
Can you show us your code?
What's wrong with using the built in file nodes?

[
    {
        "id": "ee1ab1a1d7c8b3cd",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "08fb4ab344a08be7",
        "type": "function",
        "z": "ee1ab1a1d7c8b3cd",
        "name": "",
        "func": "const fs = msg.fs\n\nvar listFilesObject = msg.payload\nvar listFiles = []\nvar listFilesName = []\nvar listFilesDir = []\nfor (var lf = 0; lf < listFilesObject.length; lf++){\n    listFiles.push(listFilesObject[lf].filename)\n    listFilesName.push(listFilesObject[lf].file)\n    listFilesDir.push(listFilesObject[lf].filedir)\n}\n\nvar readF = \"\"\n\nlistAddedObject = global.get('listAddedObject')\n\nfor (var i = 0; i < listAddedObject.length; i++){\n    var nomeFile = listAddedObject[i]['CLIENTE'] + \".json\"\n    if (listFilesName.includes(nomeFile)){\n        var index = listFilesName.indexOf(nomeFile)\n        read = fs.readFileSync(\"/volume1/ManagerSoftwares/nodeRedGestioneCommesse/database/1 Dakota.json\" ,'utf8').toString()\n        node.warn(read)\n        //readJson = JSON.parse(read)\n    } else {\n        fs.writeFile(\"/volume1/ManagerSoftwares/nodeRedGestioneCommesse/database/\" + nomeFile, \"ciao\")\n    }\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 680,
        "y": 240,
        "wires": [
            []
        ]
    },
    {
        "id": "dfdb27d2e66ea5c7",
        "type": "ui_button",
        "z": "ee1ab1a1d7c8b3cd",
        "name": "",
        "group": "f85672857eb8d277",
        "order": 11,
        "width": 0,
        "height": 0,
        "passthru": true,
        "label": "SAVE",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "",
        "payloadType": "str",
        "topic": "topic",
        "topicType": "msg",
        "x": 330,
        "y": 200,
        "wires": [
            [
                "52f183534952c516",
                "8a16e379919fe4f7"
            ]
        ]
    },
    {
        "id": "8a16e379919fe4f7",
        "type": "glob",
        "z": "ee1ab1a1d7c8b3cd",
        "name": "LIST FILES",
        "pattern": "*.json",
        "patternType": "str",
        "path": "/volume1/ManagerSoftwares/nodeRedGestioneCommesse/database",
        "pathType": "str",
        "property": "payload",
        "output": "array",
        "includeDirs": false,
        "x": 490,
        "y": 200,
        "wires": [
            [
                "08fb4ab344a08be7"
            ]
        ]
    },
    {
        "id": "52f183534952c516",
        "type": "require",
        "z": "ee1ab1a1d7c8b3cd",
        "name": "fs",
        "module": "fs",
        "field": "fs",
        "fieldType": "msg",
        "x": 490,
        "y": 260,
        "wires": [
            [
                "08fb4ab344a08be7"
            ]
        ]
    },
    {
        "id": "45293f863fdf50df",
        "type": "inject",
        "z": "ee1ab1a1d7c8b3cd",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 180,
        "y": 200,
        "wires": [
            [
                "dfdb27d2e66ea5c7"
            ]
        ]
    },
    {
        "id": "f85672857eb8d277",
        "type": "ui_group",
        "name": "",
        "tab": "2613143ac70eb817",
        "order": 1,
        "disp": false,
        "width": "10",
        "collapse": false,
        "className": ""
    },
    {
        "id": "2613143ac70eb817",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

The main problem is you are trying to use msg.fs from one path and msg.payload from a different path. The most important principle to understand with node-red is that messages from different wires will NEVER arrive at the input of a node at the same time.

Next, i see no reason to use the require node if you have node-red v2+ - as you can import fs using the setup tab on a function node.

Lastly, i see zero reason to actually use fs - you can simply use the built in read file and write file nodes to achieve this in a more graphical / low code manner.

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