# Write file node error when writing to an open csv file

**URL:** <https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479>\
**Category:** General\
**Created:** [22 July 2022 11:56 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479 "2022-07-22T11:56:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![KarolisL](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@KarolisL](https://discourse.nodered.org/u/KarolisL)\
**Post date:** [22 July 2022 11:56 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/1 "2022-07-22T11:56:10Z")

</div>

When I write to csv file when file is opened I receive error:

```auto
"failed to append to file: Error: EBUSY: resource busy or locked, open 'C:\Users\User\Desktop\xxxx.csv'"

```

When I close the file I always receive error:  
`"failed to append to file: Error [ERR_STREAM_WRITE_AFTER_END]: write after end"`

And the only way to reset this error is to restart node-red.  
This error doesn't appear with .txt files

Is there a way to check if file is opened before writing to it or any other workaround to prevent this error?

```auto
[{"id":"c466a6a63246e65b","type":"inject","z":"fa5ad0aa4db28cdf","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":640,"y":360,"wires":[["3ca7695a74dc281a"]]},{"id":"3ca7695a74dc281a","type":"function","z":"fa5ad0aa4db28cdf","name":"filename","func":"// var BATCH_NO = global.get('BATCH_NO');\n// var Assembly_NO = global.get('ASSEMBLY_NO');\n\nvar BATCH_NO = \"CNC1234\";\nvar Assembly_NO = 2;\n\nvar now = new Date();\nvar timestamp = now.getFullYear() + \"-\" + (\"0\" + (now.getMonth() + 1)).slice(-2) + \"-\" + (\"0\" + now.getDate()).slice(-2) + \" \" + (\"0\" + now.getHours()).slice(-2) + \":\" + (\"0\" + now.getMinutes()).slice(-2) + \":\" + (\"0\" + now.getSeconds()).slice(-2);\n\nmsg.filename = \"C:/Users/User/Desktop/\"+BATCH_NO+\".csv\"\n\nmsg.payload = timestamp+\",\"+BATCH_NO+\",\"+Assembly_NO;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":360,"wires":[["f0fdae733f96c3d2"]]},{"id":"f0fdae733f96c3d2","type":"file","z":"fa5ad0aa4db28cdf","name":"","filename":"filename","filenameType":"msg","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":940,"y":360,"wires":[[]]}]

```

---

<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:** [22 July 2022 12:31 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/2 "2022-07-22T12:31:58Z")

</div>

Does a Catch node catch the error? Is so then you can use that to say that something has got the file open already.

---

<div class="post-metadata">

**Author:** ![KarolisL](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@KarolisL](https://discourse.nodered.org/u/KarolisL)\
**Post date:** [22 July 2022 13:09 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/3 "2022-07-22T13:09:40Z")

</div>

Yes Catch catches an error, but it is already too late, but Write file node is already "blocked" and stops working.

Is there a way to know that this file is opened before writing to it so it won't freeze.

Everything works fine with .txt files opened in notepad.

---

<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:** [22 July 2022 13:42 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/4 "2022-07-22T13:42:50Z")

</div>

Your test flow works ok for me, though I use linux so I had to adjust the filename appropriately. When the filename is passed in, it should close the file after each write.

Can you stop node red and start it again in a terminal, click the inject button twice to generate the error and post the full terminal output here please. Copy/paste rather than screenshot please.

[Edit] Perhaps I don't understand how to make it fail. I also tried opening the file in an editor and running the flow. It did not fail, but that may because linux is cleverer than windows in this regard. Can you explain exactly what to do to make it fail please?

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [22 July 2022 14:51 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/5 "2022-07-22T14:51:43Z")

</div>

This is definitely a windows issue.

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [22 July 2022 14:58 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/6 "2022-07-22T14:58:03Z")

</div>

Try something like:

```auto
fs.open('my-file.dwg','r+', function(err,data) {

});

```

The flag `r+` causing it to return an error if permission for reading and writing isn't available

---

<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:** [22 July 2022 16:01 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/7 "2022-07-22T16:01:09Z")

</div>

Does the node lockup permanently if this happens? If so then I would have thought that is a bug that should be looked into.

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [22 July 2022 16:11 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/8 "2022-07-22T16:11:14Z")

</div>

I haven't actually had this problem, I'm only aware because I had a similar issue with a flow trying to `Watch` a log file. Worked fine on my Linux instances, but Windows was "problematic". So I can't really comment on node lockup.

---

<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:** [22 July 2022 16:29 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/9 "2022-07-22T16:29:40Z")

</div>

It would be useful if someone who uses Windows could check it out to see if is replicable in the latest node-red, if @KarolisL could describe exactly what to do with the test flow to trigger the error.

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [22 July 2022 16:56 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/10 "2022-07-22T16:56:59Z")

</div>

So far I am unable to replicate the issue on Node-RED 2.1.4 on Windows. I don't know if it depends on what program is being used to hold open the csv file or not (I'm just using VScode/Notepad++ to test).

I'll see about updating to latest.

---

<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:** [22 July 2022 17:00 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/11 "2022-07-22T17:00:22Z")

</div>

It might be worth waiting till we hear what version the OP is on, and exactly how he gets the error. I am confused by the fact that he/she appears to suggest that it is ok with .txt files, which does not make sense.

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [22 July 2022 17:02 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/12 "2022-07-22T17:02:09Z")

</div>

Yeah, the difference in file types is why I was suspicious of what program is being used.

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [22 July 2022 17:10 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/13 "2022-07-22T17:10:36Z")

</div>

I am unable to recreate the issue on Node-RED 3.0.1 as well.  
Could you provide more information please?

---

<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:** [22 July 2022 17:14 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/14 "2022-07-22T17:14:36Z")

</div>

> [@HaroldPetersInskipp](#):
>
> I was suspicious of what program is being used.

Excel possibly?

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [22 July 2022 17:18 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/15 "2022-07-22T17:18:18Z")

</div>

Aha, The issue seems to be program dependent. I can replicate the issue on v3.0.1 if the file is open in "Excel" or "OpenOffice Calc".

---

<div class="post-metadata">

**Author:** ![KarolisL](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@KarolisL](https://discourse.nodered.org/u/KarolisL)\
**Post date:** [22 July 2022 17:56 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/16 "2022-07-22T17:56:15Z")

</div>

Thanks for looking into it.

Yes, the issue happens when file is opened with excel.  
I have tried to write with this excelsheets node while file was opened

> **[node-red-contrib-excelsheets](https://flows.nodered.org/node/node-red-contrib-excelsheets)**
>
> writes multi-sheet excel files

And it worked fine.

So maybe it can be treated as a bug?

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [22 July 2022 18:08 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/17 "2022-07-22T18:08:48Z")

</div>

That node relies on `"json2excel": "^1.2.0"` as a dependency. Looking through the code, it appears there are no checks to see if the file is open. You could fork the project and submit a pull request to add it as a feature if you feel inclined.

The code you'll likely want to change is in: [json2excel/json2excel.js at master · shadowdaw/json2excel · GitHub](https://github.com/shadowdaw/json2excel/blob/master/lib/json2excel.js)

Currently it writes the file with

```auto
fs.writeFile(filepath, buffer, 'utf-8', function(err) {
    callback(err);
});

```

You would need to modify it to check if the file is in use first.

---

<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:** [22 July 2022 19:08 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/18 "2022-07-22T19:08:17Z")

</div>

Does the node lock up permanently after the error?

---

<div class="post-metadata">

**Author:** ![KarolisL](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@KarolisL](https://discourse.nodered.org/u/KarolisL)\
**Post date:** [22 July 2022 19:09 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/19 "2022-07-22T19:09:11Z")

</div>

Yes, the only solution is to restart node red, or create new node, and rewire

---

<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:** [22 July 2022 19:12 UTC](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479/20 "2022-07-22T19:12:04Z")

</div>

In that case that is the real bug. It should not lock up permanently. Generating a (Catchable) error is ok.

[Next page](https://discourse.nodered.org/t/write-file-node-error-when-writing-to-an-open-csv-file/65479.md?page=2)
