# fs.readFile, fs.writeFile, fs.readdir

**URL:** <https://discourse.nodered.org/t/fs-readfile-fs-writefile-fs-readdir/57986>\
**Category:** General\
**Created:** [8 February 2022 14:20 UTC](https://discourse.nodered.org/t/fs-readfile-fs-writefile-fs-readdir/57986 "2022-02-08T14:20:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![denissanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/denissanga/32/48346_2.png) [@denissanga](https://discourse.nodered.org/u/denissanga)\
**Post date:** [8 February 2022 14:20 UTC](https://discourse.nodered.org/t/fs-readfile-fs-writefile-fs-readdir/57986/1 "2022-02-08T14:20:39Z")

</div>

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

---

<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:** [8 February 2022 21:01 UTC](https://discourse.nodered.org/t/fs-readfile-fs-writefile-fs-readdir/57986/2 "2022-02-08T21:01:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![denissanga](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/denissanga/32/48346_2.png) [@denissanga](https://discourse.nodered.org/u/denissanga)\
**Post date:** [9 February 2022 06:41 UTC](https://discourse.nodered.org/t/fs-readfile-fs-writefile-fs-readdir/57986/3 "2022-02-09T06:41:20Z")

</div>

```auto
[
    {
        "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
    }
]

```

---

<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:** [9 February 2022 08:20 UTC](https://discourse.nodered.org/t/fs-readfile-fs-writefile-fs-readdir/57986/4 "2022-02-09T08:20:09Z")

</div>

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.

---

<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:** [10 April 2022 08:20 UTC](https://discourse.nodered.org/t/fs-readfile-fs-writefile-fs-readdir/57986/5 "2022-04-10T08:20:44Z")

</div>

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