Search code examples
node.jsstdinfscsv-write-stream

Node.js - write CSV file creates empty file in production, while OK in Mocha testing


This gist shows a code snippet that dumps an object into a CSV file. File writing is done using module csv-write-stream and it returns a promise.

This code works flawlessly in all the Mocha tests that I have made.

When the code is invoked by the main nodejs app (a server-side REPL application involving raw process.stdin and console.log invocations as interaction with the user), the CSV file is created, but it's always empty and no error/warning seems to be thrown.

I have debugged extensively the REPL code with node-debug and the Chrome dev tools: I am sure that the event handlers of the WriteStream are working properly: no 'error', all 'data' seems to be handled, 'close' runs, the promise resolves as expected.

Nevertheless, in the latter case the file is always 0 bytes. I have checked several Q&A's and cannot find anything as specific as this.

My questions are:

  • can I be missing some errors? how can I be sure to track all communications about the file write?
  • in which direction could I intensify my investigation? which other setup could help me isolate the problem?
  • since the problem may be due to the presence of process.stdin in the equation, what is a way to create a simple, light-weight interaction with the user without having to write a webapp?

I am working on Windows 7. Node 6.9.4, npm 3.5.3, csv-write-stream 2.0.0.


Solution

  • I managed to fix this issue in two ways, either by:

    • resolving the promise upon the 'finish' event of the FileWriteStream rather than on the 'end' event of the CSVWriteStream
    • removing the process.exit() I was using at the end of my operations with process.stdin (this implies that this tutorial page may be in need of some corrections)