node.js - fs dump equivalent in NodeJs? -


objective

forcing fs (and libraries using it) write files before terminating application.

background

i writing object csv file using npm package csv-write-stream.

once library done writing csv file, want terminate application using process.exit().

code

to achieve aforementioned objective, have written following:

let writer = csvwriter({   headers: ['country', 'postalcode'] });  writer.pipe(fs.createwritestream('myoutputfile.csv'));  //very big array lot of postal code info let currcountrycodes = [{country: portugal, postalcode: '2950-286'}, {country: barcelona, postalcode: '08013'}];  (let j = 0; j < currcountrycodes.length; j++) {   writer.write(currcountrycodes[j]); }  writer.end(function() {   console.log('=== csv written successfully, stopping application ===');   process.exit(); }); 

problem

the problem here if execute process.exit(), library wont have time write file, , file empty.

since library uses fs, solution problem, force fs.dump() or similar in nodejs, after searching, found nothing similar.

questions

  1. how can force fs dump (push) content file before exiting application?
  2. if first option not possible, there way wait application write , close ?

i think guess right. when call process.exit(), piped write stream hasn't finished writing yet.

if want terminate server explicitly, 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();   }); }); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -