crash-safe-write-file
Write a file atomically and use fsync to flush its contents.
Node.js file operations, even writeFileSync
, cannot guarantee that the contents of the file were written.
From http://www.daveeddy.com/2013/03/26/synchronous-file-io-in-nodejs/:
fs.writeFileSync
is synchronous in the sense that it blocks the event loop while it executes. It does NOT ask the Kernel to do a synchronous write to the underlying file system.
This is fine for most cases, but if you're trying to write a file that's immune to crashes, you need to get into the fsync
game. This library borrows graciously from the storage methods in nedb
.
Install
npm install crash-safe-write-file --save
Usage
const writeFile = require('crash-safe-write-file').writeFile;
writeFile(filename, data, callback);
import {writeFile} from 'crash-safe-write-file';
writeFile(filename, data, callback);
Signature
function writeFile(filename, data, callback);