# File writing with the file node

**URL:** https://discourse.nodered.org/t/file-writing-with-the-file-node/1585
**Category:** General
**Created:** [15 July 2018 12:02 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585 "2018-07-15T12:02:39Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [15 July 2018 12:02 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/1 "2018-07-15T12:02:39Z")

</div>

(Yeah, sorry, but I don't get it either)

Ok, next/latest problem that has me stumped.

I want to write to a file (append) in the defined httppublic directory.  
(or what ever it is called - but it is set)

This is the code for the sake of keeping it simple:

```auto
[{"id":"b52021a0.03338","type":"function","z":"b04360a6.da324","name":"Name of database + timestamp","func":"msg.filename = \"machines/timepi_temp.db\";\nvar time = Date.now();\nvar x = msg.payload;\nmsg.payload = time + ' - ' + x;\nreturn msg;","outputs":1,"noerr":0,"x":1310,"y":840,"wires":[["99a3f963.347218"]]},{"id":"99a3f963.347218","type":"file","z":"b04360a6.da324","name":"","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"false","x":1510,"y":840,"wires":[]},{"id":"edcff7e1.33b628","type":"inject","z":"b04360a6.da324","name":"","topic":"","payload":"clock","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1080,"y":840,"wires":[["b52021a0.03338"]]}]

```

Now I am not exactly sure what the "file path should be an absolute path, or else it will be relative to ........."

"absolute"?

What does that mean?

If I want to put it in node-red's public directory in "data\_base/machines" what is different to the "absolute" path?

Anyway, I run that and nothing happens in the way of errors, but the file isn't made, or I can't find it.

(true at time of posting)

After poking around:  
I have httpstatic set to /home/pi/.node-red/public  
and I use pictures from there (old story) and got the LCD display working (another story) with the fonts.

So, that part is ok.

Seems...... The file is being made in:  
/home/pi/databases/machines/

Why?

What am I missing?

---

<div class="post-metadata">

### Author: ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)
#### Post date: [15 July 2018 12:24 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/2 "2018-07-15T12:24:58Z")

</div>

Try this link [https://www.google.co.uk/search?q=absolute+file+path&ie=UTF-8&oe=UTF-8&hl=en](https://www.google.co.uk/search?q=absolute+file+path&ie=UTF-8&oe=UTF-8&hl=en)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [15 July 2018 12:34 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/3 "2018-07-15T12:34:19Z")

</div>

> [@Trying\_to\_learn](#):
>
> Now I am not exactly sure what the "file path should be an absolute path, or else it will be relative to ........."
> 
> "absolute"?
> 
> What does that mean?

This should be helpful  
[https://www.google.com/search?q=what+is+an+absolute+path+to+a+file](https://www.google.com/search?q=what+is+an+absolute+path+to+a+file)

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [15 July 2018 12:54 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/4 "2018-07-15T12:54:13Z")

</div>

Ok, so I have since discovered that "relative" paths are entered as:

from:  
[Relative vs absolute paths.](https://teamtreehouse.com/community/understanding-file-paths-relative-vs-absolute)

"../blah/and so on/what ever"

where as absolute are without the leading double dots.

Ok.

BUT!

When I go to this site:  
[Another version of the same story](https://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/)  
They say that ABSOLUTE paths are like:  
[http://www.mysite.com](http://www.mysite.com)  
and relative paths are like:  
/graphics/image.png

But then does on to say

What if you want to link to a file in a folder above the current folder? You have to tell the browser to move up one folder in your relative link by putting two periods and a slash (../) in front of the filename or path:

Which doesn't make sense if I "believe" the first link.

So back to reading.

---

<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: [15 July 2018 15:39 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/5 "2018-07-15T15:39:06Z")

</div>

> [@Trying\_to\_learn](#):
>
> where as absolute are without the leading double dots.

Not really.

Filing system absolute paths give the entire path from the "root". In Linux, the root of the filing system is always `/`. In Windows, the root is more complex and starts with a drive designation such as `c:\` or `\\networkdrive\` (there are also some more complex ones). You can also use `/` in Windows but it only refers to the root of the current or default drive.

A relative path in filing system terms starts with either , a `./` or a `../` or simply has no leading `/` at all.

The second site is talking about URL paths rather than operating system paths. The principals are the same but the leading element has to start with `protocol://location/`. These can get fairly complex.

You will probably want to do some reading on what a `URL` is and also what a `URI` is, these are often confused but there is an important difference.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [16 July 2018 03:08 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/6 "2018-07-16T03:08:31Z")

</div>

So here's the problem - for me:

This is my RPI settings.js file (part there of)

```
// that should be served at http://localhost:1880/.
//httpStatic: '/home/nol/node-red-static/',
httpStatic: '/home/pi/.node-red/public',
//httpStatic: '/home/pi',

```

Don't ask why I have left the line above and the one below. They are "backups" of the settings.

So httpStatic is set on that machine.

I want to write to a file from NR to that part and some more.

Given this code - which I know is not correct:

```auto
[{"id":"b52021a0.03338","type":"function","z":"b04360a6.da324","name":"Name of database + timestamp","func":"msg.filename = \"/home/pi/.node-red/public/databases/machines/timepi_temp.db\";\nvar time = Date.now();\nvar x = msg.payload;\nmsg.payload = time + ' - ' + x;\nreturn msg;","outputs":1,"noerr":0,"x":1400,"y":1730,"wires":[["99a3f963.347218"]]},{"id":"99a3f963.347218","type":"file","z":"b04360a6.da324","name":"","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"false","x":1600,"y":1730,"wires":[]},{"id":"edcff7e1.33b628","type":"inject","z":"b04360a6.da324","name":"","topic":"","payload":"clock","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1170,"y":1730,"wires":[["b52021a0.03338"]]},{"id":"e1c53a12.82f458","type":"function","z":"b04360a6.da324","name":"Name of database + timestamp","func":"msg.filename = \"../databases/machines/timepi_temp.db\";\nvar time = Date.now();\nvar x = msg.payload;\nmsg.payload = time + ' - ' + x;\nreturn msg;","outputs":1,"noerr":0,"x":1400,"y":1780,"wires":[["7aa3bcb6.c846bc"]]},{"id":"7aa3bcb6.c846bc","type":"file","z":"b04360a6.da324","name":"","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"false","x":1600,"y":1780,"wires":[]},{"id":"af19af1a.2ae83","type":"inject","z":"b04360a6.da324","name":"","topic":"","payload":"clock","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":1170,"y":1780,"wires":[["e1c53a12.82f458"]]}]

```

The top one works. The other doesn't create the file in the right place.

I've tried all sorts of things trying to get it to "honour" the httpStatic path and append it (as it says in the node info) but can't work out what is going wrong.

I've tried the leading /, no leading / double .. at the start and a few other things.

---

<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: [16 July 2018 06:07 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/7 "2018-07-16T06:07:38Z")

</div>

I think that you are confusing the "current" location for the second example. When NR starts, it will have its own current location that the function node inherits. Trying to second guess relative paths when inside an app is hard which is why you are generally recommended to use absolute paths.

If I remember rightly, NR sets the current folder to your userDir folder, usually `/home/pi/.node-red` on the Pi if you have a standard installation - though you can move that of course.

So

```
./public/databases/machines/timepi_temp.db

```

Would likely be the correct relative location.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [16 July 2018 20:27 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/8 "2018-07-16T20:27:19Z")

</div>

> [@TotallyInformation](#):
>
> If I remember rightly, NR sets the current folder to your userDir folder, usually `/home/pi/.node-red`

Actually it sets it to `/home/pi`. That is done in the systemd service `/lib/systemd/system/nodered.service`. If the user that ran the update script is not `pi` then it is set to that users home directory.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [16 July 2018 22:31 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/9 "2018-07-16T22:31:49Z")

</div>

Thanks both.

So where does the httpStatic path come into play?

---

<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: [16 July 2018 22:38 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/10 "2018-07-16T22:38:21Z")

</div>

> [@Trying\_to\_learn](#):
>
> So where does the httpStatic path come into play?

The `httpStatic` setting has absolutely nothing to do with the File node.

As described before, the `httpStatic` setting identifies a directory that Node-RED will serve up as static content. That is the only thing it does with that setting.

If you want the File node to write into the directory identified by `httpStatic`, you'll need to give it the same full path.

So if you have `httpStatic` set to `/home/pi/public` and you want the File node to create a file under there, you'll need to make sure the path you give the File node has `/home/pi/public` at the start.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [16 July 2018 23:02 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/11 "2018-07-16T23:02:26Z")

</div>

Thanks.

I didn't "get" that originally. Confusion? Maybe. But what ever it is/was, I missed it.

Maybe because when I was asking about that it was for HTTP stuff rather than local stuff.

So there is nothing I can set to make the "home directory for NR" a specified path so when I enter paths, it starts there?

---

<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: [16 July 2018 23:18 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/12 "2018-07-16T23:18:28Z")

</div>

> [@Trying\_to\_learn](#):
>
> So there is nothing I can set to make the "home directory for NR" a specified path so when I enter paths, it starts there?

It is possible to edit the `nodered.service` file to change the working directory of the Node-RED process - as Colin mentioned [here](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/8).

But, in my opinion, it's better to be explicit and use the full, absolute, path name in your flow.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [16 July 2018 23:30 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/13 "2018-07-16T23:30:23Z")

</div>

Thanks again.

As I am sure I have said many times.

It isn't I am trying to be difficult. I am just wanting to learn. Sometimes I forget or don't "Get" what is said.

I shall probably stick to the entire path.

It was more just for the sake of checking and wondering if it is/was possible.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [17 July 2018 07:58 UTC](https://discourse.nodered.org/t/file-writing-with-the-file-node/1585/14 "2018-07-17T07:58:48Z")

</div>

> [@Trying\_to\_learn](#):
>
> I shall probably stick to the entire path.

I think that is probably the best way to do it anyway.
