Prevent file corruption on exiting node in middle of fs.writeFile

user1165273

I'm reading and writing some txt/json files in Node.js that have become >25mb in file size. If the process is interrupted half way through an fs.writeFile I get an empty file. So basically, I'm wondering if anyone knows of a quick way to prevent file writes from being corrupted if the process is exited. Is there a way to catch the exit and wait until the fs.writeFile callback is called?

Troy Gizzi

A common approach is to first write to a different file (e.g., instead of overwriting permanent-file.txt directly, write to temporary-file.txt), and then in the callback from fs.writeFile, use fs.rename to replace the permanent file with the temporary one (e.g., fs.rename('temporary-file.txt', 'permanent-file.txt');).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

node.js axios download file stream and writeFile

From Dev

node.js fs.writeFile Not Completely Overwriting File

From Dev

Access name of file being written by fs.writeFile

From Dev

Node.js fs.writeFile() not ovewriting file

From Dev

Node fs showing that file is not directory when it is

From Dev

How to ensure all directories exist before to fs.writeFile using node.js

From Dev

ENOENT error when using fs.writeFile

From Dev

Node fs.readdir not working in Grunt file

From Dev

fs writefile new line not working

From Dev

Why is the following fs.writeFile only writing data from the last file?

From Dev

Promise prevent node process from exiting

From Dev

Node.JS WriteFile - File of specific size

From Dev

node - fs.writeFile creates a blank file

From Dev

fs.writefile only execute in last function in node js

From Dev

Why is Node fs.writeFile() method successful, but an empty file is then sent to the browser?

From Dev

Looks like when I do fs.writeFile(), the changed file restarts nodemon. How to make it not restart?

From Dev

fs.WriteFile() returning NULL and not writing to JSON file

From Dev

Node fs showing that file is not directory when it is

From Dev

Prevent WindowsService exiting

From Dev

fs writefile new line not working

From Dev

Node.JS WriteFile - File of specific size

From Dev

fs.writefile only execute in last function in node js

From Dev

Running `fs.writeFile` with JSPM

From Dev

node's fs.writeFile does not overwrite previous contents

From Dev

Error with NodeJS FS (writeFile)

From Dev

Using fs.stat and fs.writeFile

From Dev

How to use node js util.promisify with the writeFile fs function and await

From Dev

fs.writeFileSnyc/fs.writeFile writes a corrupted file

From Dev

Prevent page from exiting

Related Related

  1. 1

    node.js axios download file stream and writeFile

  2. 2

    node.js fs.writeFile Not Completely Overwriting File

  3. 3

    Access name of file being written by fs.writeFile

  4. 4

    Node.js fs.writeFile() not ovewriting file

  5. 5

    Node fs showing that file is not directory when it is

  6. 6

    How to ensure all directories exist before to fs.writeFile using node.js

  7. 7

    ENOENT error when using fs.writeFile

  8. 8

    Node fs.readdir not working in Grunt file

  9. 9

    fs writefile new line not working

  10. 10

    Why is the following fs.writeFile only writing data from the last file?

  11. 11

    Promise prevent node process from exiting

  12. 12

    Node.JS WriteFile - File of specific size

  13. 13

    node - fs.writeFile creates a blank file

  14. 14

    fs.writefile only execute in last function in node js

  15. 15

    Why is Node fs.writeFile() method successful, but an empty file is then sent to the browser?

  16. 16

    Looks like when I do fs.writeFile(), the changed file restarts nodemon. How to make it not restart?

  17. 17

    fs.WriteFile() returning NULL and not writing to JSON file

  18. 18

    Node fs showing that file is not directory when it is

  19. 19

    Prevent WindowsService exiting

  20. 20

    fs writefile new line not working

  21. 21

    Node.JS WriteFile - File of specific size

  22. 22

    fs.writefile only execute in last function in node js

  23. 23

    Running `fs.writeFile` with JSPM

  24. 24

    node's fs.writeFile does not overwrite previous contents

  25. 25

    Error with NodeJS FS (writeFile)

  26. 26

    Using fs.stat and fs.writeFile

  27. 27

    How to use node js util.promisify with the writeFile fs function and await

  28. 28

    fs.writeFileSnyc/fs.writeFile writes a corrupted file

  29. 29

    Prevent page from exiting

HotTag

Archive