Node.js Overwriting a file

15,886

Solution 1

From the docs:

Modifying a file rather than replacing it may require a flags mode of r+ rather than the default mode w.

So {flags: 'w'} should work. This sounds like a permissions issue?

Are you able to do an fs.unlink() on that file? This should test the permissions on that file if you don't have access to the computer directly.

Solution 2

Isn't the flag 'w' set by default ? https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options

Share:
15,886
Michael Yagudaev
Author by

Michael Yagudaev

Entrepreneur, founder of nano3labs, Web application developer, passionate about Javascript, React, Swift and Mobile development. Trying to help others, both online and offline. Feel free to drop me a line :).

Updated on June 04, 2022

Comments

  • Michael Yagudaev
    Michael Yagudaev almost 2 years

    I know it is a simple question, but haven't been able to find anything useful on this one.

    How do you use fs.createWriteStream(dst) to overwrite a file? (note that the app is hosted on heroku)

    I tried {flags: 'w'} or {flags: 'r+'} even {flags: 'wb'}. None of these worked, I keep getting Error: File uploads/1.txt exists.