FAQ: Learn Node.js - Writable Streams

This community-built FAQ covers the “Writable Streams” exercise from the lesson “Learn Node.js”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Node.js

FAQs on the exercise Writable Streams

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I tried doing something like this:

const readline = require(‘readline’);
const fs = require(‘fs’);

const myInterface = readline.createInterface({
input: fs.createReadStream(‘shoppingList.txt’)
});

const fileStream = fs.createWriteStream(“shoppingResults.txt”);

var transformData = function(data){
fileStream.write(They were out of: ${data}\n);
fileStream.end();
};

myInterface.on(“line”, transformData);

But fileStream.end() threw an error. Why is fileStream.end() not necessary for this exercise?

I can figure fileStream.end is not necessary because the write stream is a listener callback function for a ‘line’ event emmited from the read stream which ends by itself.
The explanation tells: “Unlike a readable stream, which ends when it has no more data to read, a writable stream could remain open indefinitely.”

I can’t open ‘shoppingResults.txt’. Previous steps was ok! but in terminal nothing happens after type ‘node app.js’. And after press ‘check work’ this step shows like done :thinking:

2 Likes

@alex_j1 what issue did you have? did you check the folder icon and you didn’t see 'shoppingResults.txt’? or you can see it but it doesn’t open?

I just experienced this after playing around with my code. To fix it, I went to the little folder icon above the editor, the i deleted the 'shoppingResults.txt’ file. After running the code in the terminal,if your code is correct, the 'shoppingResults.txt’ file would be added back to the folder and you should be able to open it .

After running the program,nothing will happen in the terminal because we never outputted anything to the terminal. So you’d only see results of the code in “shoppingResults.txt” file

@dom59 I tried this and it gave the error: “Write after end”. When I checked my 'shoppingResults.txt’ file, I noticed only the first line was added. I think the reason for this is we are using “line” events to write data to the 'shoppingResults.txt’ file, so adding a .end() inside of the callback function tells the program to write a line the close/end the stream after that line. Hence when the next line tries to run, we’ve already closed/ended the stream to that file,so it outputs the error: “write after end”.
.
The example is quite different since we are writing lines directly to a file from the editor.
That aside it is important to end a stream when writing to a file. The importance of this is seen in an app like MS-Word. If you are editing a document in MS-Word and you don’t close the program when you’re done, you’ll notice you’d be unable to move/copy the MS-Word file to another device like your phone.

1 Like

Okay so I tried running the app with the node app.js and it obviously ran but with nothing to display. I reset the exercise (at least I got more practice) and same… Though after reading through this because I was like ??? neither file had any content in it? Can’t display what isn’t there??! Though it would be cool if they added a cool treasure hunt back into this like they did in the previous example. 10/10 would search for the magical mysterious node provisioning list.

myInterface.on(‘line’, transformData)
where does the event name come from. I tried changing it and it doesnt work unless the event name is ‘line’

Once you type node app.js, it will create a file in the file browser called shoppingResults.txt, it won’t display in the bash output.

I’m having the same issue.
Partly I think it’s that I misundertand what it’s meant to be doing.

My code is identical to the supplied solution, I am getting a ‘done’.

However I don’t understand what is happening.

Is the purpose to make a copy of ‘shoppingList.txt’?

1 Like

It’s mostly to show that can create a file that you make with fs.createFileStream(), and the parameter, in string, is the name of the file you wish to create. I like to think of it a coding a ‘Save As’ function.

In this exercise you read items from one file, transformed the data with a factory function and then saved the output in string format to a newly created txt file using fs.createFileStream().