# How to pass path and file to file-in node from a object?

**URL:** <https://discourse.nodered.org/t/how-to-pass-path-and-file-to-file-in-node-from-a-object/59504>\
**Category:** General\
**Created:** [7 March 2022 17:13 UTC](https://discourse.nodered.org/t/how-to-pass-path-and-file-to-file-in-node-from-a-object/59504 "2022-03-07T17:13:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gitsaibot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gitsaibot/32/58008_2.png) [@Gitsaibot](https://discourse.nodered.org/u/Gitsaibot)\
**Post date:** [7 March 2022 17:13 UTC](https://discourse.nodered.org/t/how-to-pass-path-and-file-to-file-in-node-from-a-object/59504/1 "2022-03-07T17:13:56Z")

</div>

Hi guys, I am a NodeRed newbie and need your help. I want to pass path and file as an absolute filename to a "read file" node. But I always got errors like this:  
Error: ENOENT: no such file or directory, open 'undefined[object Object]'"

Here is my flow:

```auto
[
    {
        "id": "60463e3a.b7fb4",
        "type": "tab",
        "label": "graph csv",
        "disabled": false,
        "info": ""
    },
    {
        "id": "ff0ba1e2.f46ab",
        "type": "ui_chart",
        "z": "60463e3a.b7fb4",
        "name": "",
        "group": "2ac8e3fa.8b8584",
        "order": 5,
        "width": 0,
        "height": 0,
        "label": "chart",
        "chartType": "line",
        "legend": "true",
        "xformat": "HH:mm:ss",
        "interpolate": "linear",
        "nodata": "",
        "dot": false,
        "ymin": "",
        "ymax": "",
        "removeOlder": "2",
        "removeOlderPoints": "1000",
        "removeOlderUnit": "3600",
        "cutout": 0,
        "useOneColor": false,
        "useUTC": true,
        "colors": [
            "#1f77b4",
            "#aec7e8",
            "#ff7f0e",
            "#2ca02c",
            "#98df8a",
            "#d62728",
            "#ff9896",
            "#9467bd",
            "#c5b0d5"
        ],
        "outputs": 1,
        "x": 550,
        "y": 500,
        "wires": [
            []
        ]
    },
    {
        "id": "2466e014.fe459",
        "type": "debug",
        "z": "60463e3a.b7fb4",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 530,
        "y": 320,
        "wires": []
    },
    {
        "id": "f6d3c9af.1c6d18",
        "type": "file in",
        "z": "60463e3a.b7fb4",
        "name": "file path",
        "filename": "",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 200,
        "y": 320,
        "wires": [
            [
                "3d784d1.a733332",
                "2466e014.fe459"
            ]
        ]
    },
    {
        "id": "3d784d1.a733332",
        "type": "csv",
        "z": "60463e3a.b7fb4",
        "name": "",
        "sep": ",",
        "hdrin": true,
        "hdrout": "",
        "multi": "mult",
        "ret": "\\n",
        "temp": "Test, Test2, Test3, Test4",
        "skip": "0",
        "strings": true,
        "include_empty_strings": false,
        "include_null_values": false,
        "x": 310,
        "y": 400,
        "wires": [
            [
                "f3d8e5f0.29d548",
                "2466e014.fe459"
            ]
        ]
    },
    {
        "id": "f3d8e5f0.29d548",
        "type": "function",
        "z": "60463e3a.b7fb4",
        "name": "graph generate",
        "func": "\nfunction toTimestamp(strDate){\n var datum = Date.parse(strDate);\n return datum/1000;\n }\nvar payload=msg.payload;\n\n\nvar data_out=[];\nvar a1=[];\nvar a2=[];\nvar a3=[];\nvar a4=[];\nfor(let i=0;i<payload.length;i++)\n{\n \nvar Data= payload[i].Data.split(\"-\");\nvar t=Data[2]+\"/\"+Data[1]+\"/\"+Data[0];\nvar timestamp=toTimestamp(t+\" \"+payload[i][\"Godzina\"]);\n\n\nvar temp1={ \"x\": timestamp, \"y\":parseInt( payload[i][\"Temp_RS-485\"]) };\nvar temp2={ \"x\": timestamp, \"y\":parseInt( payload[i][\"Wilg_RS-485\"]) };\nvar temp3={ \"x\": timestamp, \"y\": parseInt(payload[i][\"Temperatura I\"])};\nvar temp4={\"x\": timestamp, \"y\": parseInt(payload[i][\"Wilgotność I\"])};\n\na1.push(temp1);\na2.push(temp2);\na3.push(temp3);\na4.push(temp4);\n \n \n //\n \n \n}\n\ndata_out=[a1,a2,a3,a4];\nmsg.payload=[{\n\"series\": [\"Temp_RS-485\",\"Wilg_RS-485\",\"Temperatura I\",\"Wilgotność I\"],\n\"data\":data_out,\n\"labels\": [\"Temp_RS-485\",\"Wilg_RS-485\",\"Temperatura I\",\"Wilgotność I\"]\n}];\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 320,
        "y": 520,
        "wires": [
            [
                "2466e014.fe459",
                "ff0ba1e2.f46ab"
            ]
        ]
    },
    {
        "id": "6456763b.e07d78",
        "type": "fs-ops-dir",
        "z": "60463e3a.b7fb4",
        "name": "",
        "path": "payload",
        "pathType": "msg",
        "filter": "*.csv",
        "filterType": "str",
        "dir": "files",
        "dirType": "msg",
        "x": 320,
        "y": 100,
        "wires": [
            [
                "35811671.90ebea",
                "572a14c4.62aa3c"
            ]
        ]
    },
    {
        "id": "35811671.90ebea",
        "type": "fs-ops-stats",
        "z": "60463e3a.b7fb4",
        "name": "",
        "path": "payload",
        "pathType": "msg",
        "filename": "files",
        "filenameType": "msg",
        "stats": "stats",
        "sizeType": "msg",
        "x": 480,
        "y": 100,
        "wires": [
            [
                "20ef5f9.b8d63a",
                "ee6515ea.f968c8"
            ]
        ]
    },
    {
        "id": "7dbdf6ef.a73868",
        "type": "inject",
        "z": "60463e3a.b7fb4",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "/home/pi/csv/",
        "payloadType": "str",
        "x": 130,
        "y": 100,
        "wires": [
            [
                "6456763b.e07d78"
            ]
        ]
    },
    {
        "id": "dd9228cf.983768",
        "type": "debug",
        "z": "60463e3a.b7fb4",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "combined",
        "targetType": "msg",
        "x": 880,
        "y": 160,
        "wires": []
    },
    {
        "id": "572a14c4.62aa3c",
        "type": "debug",
        "z": "60463e3a.b7fb4",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "files",
        "targetType": "msg",
        "x": 460,
        "y": 160,
        "wires": []
    },
    {
        "id": "20ef5f9.b8d63a",
        "type": "debug",
        "z": "60463e3a.b7fb4",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "stats",
        "targetType": "msg",
        "x": 660,
        "y": 160,
        "wires": []
    },
    {
        "id": "ee6515ea.f968c8",
        "type": "function",
        "z": "60463e3a.b7fb4",
        "name": "Combine and sort",
        "func": "var combined = [];\n\nfor (var i = 0; i < msg.files.length; i++) {\n var file = msg.files[i];\n var stat = msg.stats[i];\n \n combined.push({\n path: msg.payload,\n file,\n ...stat\n })\n}\n\ncombined.sort((a, b) => b.birthtimeMs - a.birthtimeMs);\n\nmsg.combined = combined;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 670,
        "y": 100,
        "wires": [
            [
                "dd9228cf.983768",
                "50034043.e2347"
            ]
        ]
    },
    {
        "id": "50034043.e2347",
        "type": "change",
        "z": "60463e3a.b7fb4",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "combined[1]",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 880,
        "y": 100,
        "wires": [
            [
                "4f9068e.e12c498",
                "b4310deac4a54284"
            ]
        ]
    },
    {
        "id": "4f9068e.e12c498",
        "type": "debug",
        "z": "60463e3a.b7fb4",
        "name": "newest CSV",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1090,
        "y": 100,
        "wires": []
    },
    {
        "id": "25428842.245c68",
        "type": "comment",
        "z": "60463e3a.b7fb4",
        "name": "Directory gets list of CSVs. File Stats gets their details. combine and sort puts data together and sorts. Switch node takes 1st item and puts it in payload",
        "info": "",
        "x": 640,
        "y": 40,
        "wires": []
    },
    {
        "id": "b4310deac4a54284",
        "type": "function",
        "z": "60463e3a.b7fb4",
        "name": "make filename",
        "func": "msg.file = msg.payload;\nmsg.filename = msg.path + \"\\\\\" + msg.payload;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 620,
        "y": 240,
        "wires": [
            [
                "f6d3c9af.1c6d18",
                "9fe83a8a2f3e7d01"
            ]
        ]
    },
    {
        "id": "9fe83a8a2f3e7d01",
        "type": "debug",
        "z": "60463e3a.b7fb4",
        "name": "make file",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 890,
        "y": 280,
        "wires": []
    },
    {
        "id": "2ac8e3fa.8b8584",
        "type": "ui_group",
        "name": "Default",
        "tab": "ebb1ed1c.5ebe2",
        "order": 1,
        "disp": true,
        "width": "12",
        "collapse": false
    },
    {
        "id": "ebb1ed1c.5ebe2",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

```

---

<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:** [7 March 2022 17:18 UTC](https://discourse.nodered.org/t/how-to-pass-path-and-file-to-file-in-node-from-a-object/59504/2 "2022-03-07T17:18:30Z")

</div>

> [@Gitsaibot](#):
>
> Error: ENOENT: no such file or directory, open 'undefined[object Object]'"

You have made an error when composing the filename. `undefined[object Object]` is obviously not a valid filename.

---

<div class="post-metadata">

**Author:** ![Gitsaibot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gitsaibot/32/58008_2.png) [@Gitsaibot](https://discourse.nodered.org/u/Gitsaibot)\
**Post date:** [7 March 2022 17:29 UTC](https://discourse.nodered.org/t/how-to-pass-path-and-file-to-file-in-node-from-a-object/59504/3 "2022-03-07T17:29:41Z")

</div>

I know 😉 but didn't find a working solution. My object looks like this:

```auto
msg.payload : Object
object
path: "/home/pi/csv/"
file: "example.csv"
dev: 45826
mode: 33188
nlink: 1
uid: 1000
gid: 1000
rdev: 0
blksize: 4096
ino: 4092

```

and the function node where I try to extract the full path is here:

```auto
msg.file = msg.payload;
msg.filename = msg.path + "\\" + msg.payload;
return msg;

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [7 March 2022 17:29 UTC](https://discourse.nodered.org/t/how-to-pass-path-and-file-to-file-in-node-from-a-object/59504/4 "2022-03-07T17:29:50Z")

</div>

@Gitsaibot Can you expand a bit more on what your flow is doing? How are you generating the filename? where do the values come from?

Have you used a Debug node to see what you are setting it to?

---

<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:** [7 March 2022 17:34 UTC](https://discourse.nodered.org/t/how-to-pass-path-and-file-to-file-in-node-from-a-object/59504/5 "2022-03-07T17:34:18Z")

</div>

if msg looks like:

```auto
{
   "payload": {
      path: "/home/pi/csv/",
      file: "example.csv",
      dev: 45826,
      mode: 33188,
      nlink: 1,
      uid: 1000,
      gid: 1000,
      rdev: 0,
      blksize: 4096,
      ino: 4092,
   }
}

```

The filename would be:

```auto
msg.filename = msg.payload.path + "/" + msg.payload.file;

```

Note the fwd slash not back-slashes since your path indicates that you are on a Pi which uses Linux.

Better still might be:

```auto
const path = require('path') // this is a node.js built-in library
msg.filename = path.join( msg.payload.path, msg.payload.file );

```

Since that takes care of differences between Windows, Mac and Linux file systems.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [7 March 2022 17:45 UTC](https://discourse.nodered.org/t/how-to-pass-path-and-file-to-file-in-node-from-a-object/59504/6 "2022-03-07T17:45:55Z")

</div>

> [@TotallyInformation](#):
>
> `const path = require('path')`

Although you cannot use `require` in the Function node.

---

<div class="post-metadata">

**Author:** ![Gitsaibot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gitsaibot/32/58008_2.png) [@Gitsaibot](https://discourse.nodered.org/u/Gitsaibot)\
**Post date:** [7 March 2022 17:46 UTC](https://discourse.nodered.org/t/how-to-pass-path-and-file-to-file-in-node-from-a-object/59504/7 "2022-03-07T17:46:56Z")

</div>

> Note the fwd slash not back-slashes since your path indicates that you are on a Pi which uses Linux.

You are right, that was a stupid mistake 😉 Your first solution works for me, but when I try your second suggestion I get this error:

"ReferenceError: require is not defined (line 1, col 14)"

---

<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:** [7 March 2022 17:54 UTC](https://discourse.nodered.org/t/how-to-pass-path-and-file-to-file-in-node-from-a-object/59504/8 "2022-03-07T17:54:11Z")

</div>

> [@knolleary](#):
>
> Although you cannot use `require` in the Function node.

Oh blast - I knew there was something wrong with writing that! Long day.

This works of course:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/283d2746c724afdc8fd1152119109ba62b1223d0.png)

> [@Gitsaibot](#):
>
> "ReferenceError: require is not defined (line 1, col 14)"

Yes, as Nick points out 🌩

Do the above and change the code to just:

```auto
msg.filename = path.join( msg.payload.path, msg.payload.file )

```

---

<div class="post-metadata">

**Author:** ![Gitsaibot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gitsaibot/32/58008_2.png) [@Gitsaibot](https://discourse.nodered.org/u/Gitsaibot)\
**Post date:** [7 March 2022 17:59 UTC](https://discourse.nodered.org/t/how-to-pass-path-and-file-to-file-in-node-from-a-object/59504/9 "2022-03-07T17:59:38Z")

</div>

Yes it works! Thanks for the quick and detailed help

---

<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:** [21 March 2022 17:59 UTC](https://discourse.nodered.org/t/how-to-pass-path-and-file-to-file-in-node-from-a-object/59504/10 "2022-03-21T17:59:47Z")

</div>

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