New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Node.js Readline not writting to output file #1292
Comments
You are not writing the 'line' on the writeStream that you've opened. That is, the write() method on ReadLine is a way of adding data into the input buffer as documented thus: "The rl.write() method will write the data to the readline Interface's input as if it were provided by the user." You probably mean something like:
|
I guess I understand it better now. However, if I do it that way, in order to preserver the order of the logs, I need to resume and pause the Readline stream, correct?
Like this? Also, I know I missed that part in the docs, but I fail to see why I would want my write method to feed input into the next iteration of my read event.... Is there a good use case for this? It mainly sounds counter-intuitive ( at least to me ), that's all. |
Are you actually finding that the order of your output is being distorted? I didn't observe this (yet) although I'm certainly not testing with large input files. However, you could certainly use those controls on readLine to govern the rate of consumption/transformation. You might also wish to only being processing the |
I'll have to defer to others on that point! However, I expect that there is, otherwise they would not have created the option... And, I believe it probably relates to when used with a TTY, rather than with files. |
No, the order will be preserved with or without pausing the stream. |
How if |
Yes, but there is an internal buffer which stores the data in the same order in which the data came in. |
Ahh, thank you for the feedback. |
You don't need to wait as long as you write the data in separate asynchronous calls and as long as the "other stuff" that you do between reading a line and writing the line is synchronous. For example, the following will crash the application for large data: var fs = require('fs');
var stream = fs.createWriteStream('test.txt');
while(1)
stream.write('some data'); because the internal buffer can't be freed until you return the control to the event loop. However, the following code works fine: setInterval(() => {
stream.write('some data');
}); because you're writting data from the function that is called asynchronously by Now, back to your code. You example works fine if you replace rl.on('line', line => {
//Do your stuff ...
var transformedLine = line.toUpperCase();
console.log(transformedLine);
//Then write to outstream
writeStream.write(transformedLine + '\n');
}); However, if you call rl.on('line', line => {
//Do your stuff ...
var transformedLine = line.toUpperCase();
console.log(transformedLine);
setTimeout(() => {
//Then write to outstream
writeStream.write(transformedLine + '\n');
});
}); And also, notice that if you want to write the data in separate lines, you need to add |
Got it ! |
Background
Dear awesome people,
I am trying to read a several GB sized file line by line. I want to process each line and after that write it to a file. I don't want to ( nor can I ) put everything into memory.
It is important that the order in which i read a line is the order in which I write it to a file.
Code
To achieve this I tried using Node.js Readline interface
Problem
As you can see, I am trying to read a line, parse it, and write it into a file called
out.csv
.The problem is that the output file is always empty. Nothing is ever written into it.
I have read all the methods, events and options, but clearly I am missing something.
Question
Why is this code not writing into the file?
The text was updated successfully, but these errors were encountered: