Forcing fs (and the libraries using it) to write everything to files before terminating application.
I am writing an object to a CSV file using the npm package csv-write-stream.
Once the library is done writing the CSV file, I want to terminate my application using process.exit()
.
To achieve the aforementioned objective, I have written the following:
let writer = csvWriter({
headers: ['country', 'postalCode']
});
writer.pipe(fs.createWriteStream('myOutputFile.csv'));
//Very big array with a lot of postal code info
let currCountryCodes = [{country: Portugal, postalCode: '2950-286'}, {country: Barcelona, postalCode: '08013'}];
for (let j = 0; j < currCountryCodes.length; j++) {
writer.write(currCountryCodes[j]);
}
writer.end(function() {
console.log('=== CSV written successfully, stopping application ===');
process.exit();
});
The problem here is that if I execute process.exit()
, the library wont have time to write to the file, and the file will be empty.
Since the library uses fs
, my solution to this problem, is to force a fs.dump()
or something similar in NodeJs, but after searching, I found nothing similar.
I think your guess is right.
When you call process.exit()
, the piped write stream hasn't finished writing yet.
If you really want to terminate your server explicitly, this will do.
let r = fs.createWriteStream('myOutputFile.csv');
writer.pipe(r);
...
writer.end(function() {
r.end(function() {
console.log('=== CSV written successfully, stopping application ===');
process.exit();
});
});