Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single file, being written by multiple users in nodejs

Tags:

node.js

I have multiple users logged in at the same time, and they can write the same file simultaneously. How can I prevent collision for a file when multiple users are writing on a single file in nodejs.

like image 942
Umer Avatar asked Jun 20 '26 08:06

Umer


1 Answers

Assuming you only have one node process the simples solution would be to use fs.writeFileSync.

The proper way to do it is to use rwlock to properly lock file so that only one process at a time can write to it.

like image 195
Łukasz Avatar answered Jun 21 '26 22:06

Łukasz