I'm playing with Node.js fs.writeFile() flags to find the right file for my problem: I want to replace the content of a file but throw an error if the file does not exist.
My first try was with r+ but I have issues if the new content is shorter than the old one:
fs.writeFileSync('test', '11111111111');
> 111111111111
fs.writeFileSync('test', '22', {flag: 'r+'})
> 221111111111
Removing the flag solve the problem (give 22) but create a new file if test doesn't exist.
Is it doable with a flag or do I need to detect file existence before (not very found of that)?