Write file node error when writing to an open csv file

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

"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?

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

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

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.

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?

This is definitely a windows issue.

Try something like:

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

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

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.

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.

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.

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.

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

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

Excel possibly?

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".

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

And it worked fine.

So maybe it can be treated as a bug?

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

Currently it writes the file with

fs.writeFile(filepath, buffer, 'utf-8', function(err) {

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

Does the node lock up permanently after the error?

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

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