Dave Gray · Transcript · · Updated
Reading and Writing Files with Node.js | Node JS Beginners Tutorial – Transcript
Video by Dave Gray · Watch the original on YouTube
Summary
AI-generated from the transcript. Verify details against the source video.
- Dave explains that the file system common core module in Node.js allows users to create, read, update, and delete files as well as work with servers and directories.
- Dave demonstrates how to handle file operations using asynchronous methods like read file, write file, and append file along with error handling.
- Dave illustrates how to avoid callback hell in Node.js by utilizing fs promises with async and await for cleaner file operations.

Use this video with Pro
Reading, copying and transcript downloads are free. Create saved AI results for this video with a signed-in Pro account ($9/month).
Transcript
Search ignores accents. All transcript text stays visible.
hello and welcome hi i'm dave and this tutorial is part of a learn node tutorial series for beginners i'll give a link to the full playlist in the description below today we're going to look at the file system common core module for node.js it allows us to create read update and delete files and work with directories on the server and that's because node.js is a javascript runtime that actually runs on the server
instead of in the browser now i'm going to click docs at nodejs.org because nodejs.org is the source of truth for information about node much like mdn would be the source of truth for information about vanilla javascript as well as html and css currently the stable version that i have installed is 14.17.5 as you view this tutorial the versions that are stable may change so
go ahead and click on whichever version you have and now you can see we have node.js documentation today we're looking at the file system module so i can click that right here and there are many things you can do with the file system with directories and different files so amongst all of those things what i usually do is press control f i'm on windows and then i type what i'm looking for and we're going to start out by reading files so when i type read file i
can see the fs dot read file and it shows the structure here for what i want to call and then you can click it and get details about that i just wanted to show you how to find the different documentation on node.js and now let's get started in visual studio code as we work with the fs the file system common core module i've got visual studio code open with an empty index.js file i've
also got a files directory here in the file tree and you can see a couple of starter text files a lorem text file that has a lot of text in it and a starter text file that just says hi my name is dave we're going to start by importing the fs common core module here and we use common js imports for that and so it's just require fs
after that let's go ahead and read that starter file so we need to specify the file and it's in files and then it's at starter dot text from there we have a callback function that has the error and data that we read and so we'll say if error we need to throw the error and otherwise let's log the data to the
console now if you remember the console in node.js is in the terminal so i'm going to press control and backtick to open a terminal window you could also do that from the terminal menu now here to run our little node file in index we just type node and then index we don't need the js and let's go ahead and take a look at this data from starter
and notice after we read the data it's presented as buffer data so that's what we get right here if we want to be able to read the data we can put a two string method afterwards i'll save and i'll go ahead and call this again and now we can see hi my name is dave in the console but instead of two string we can also put in a parameter here that says
the encoding or defines the encoding so we'll say utf-8 before the callback as a perimeter and now let's go ahead and run it again node index in the terminal and once again we get hi my name is dave now notice the throw error here according to the node documentation if we get an uncaught exception we need to go ahead and catch that and i'm just going to paste this in
if we have an uncaught error we should exit so we listen for this uncaught exception using process now process is one of those values that node has available to us we don't need to import it it's already there so we're listing for an uncaught exception then we pass in the error to the callback here and we're just logging the console error there was an uncaught error and then we put the error there and then we
exit the application and this is direct from the node documentation so let's go ahead and throw an error on purpose i'll just look for a file to read that doesn't exist named hello and once again we'll call node index and you can see we get an uncaught error and here it is there is no such file or directory now let's change this back to starter so we can keep reading the data that exists
and i'm going to put a console log statement underneath and this is to demonstrate that read file and node in general the functions or methods you'll find from node will be asynchronous and so we're logging the data here but we're also logging hello here and node has the ability to say i'm going to process this but let's go ahead
and tackle the rest of the tasks in the program and when i finish reading the file i'll get that data to you if you're familiar with async a weight this should be a familiar concept but let's go ahead and save this and let's see which we get in the console first hello or the data and we get hello and then we get hi my name is dave so node said it would read the file but it went ahead and processed
this console log statement and it actually was logged to the console first and then when node completed reading the file it logged the data to the console instead of hard coding the path like we see here in read file there is a better way and let's pull in the path module to do that so we have path and then we use require and we're requiring path and the reason is the slashes sometimes
if you're familiar with different operating systems sometimes backwards sometimes forward there can be some problems not always but there can be when you hard code file paths like that but if we use the path module we can eliminate this problem so let's use path dot join i typed json there we go join and then we need to specify first the directory name and that's two underscores first and remember that's a
value that's always available to us in node after the directory name we'll say the files directory to attach on to concatenate to that directory name and then we need to say the actual name of the file itself so now we've got starter text so if we supply this instead of hard coding a file name it's a much better approach let's go ahead
and save and run this one more time and everything still works as it did now that we know how to read files and catch an error let's go ahead and copy the read file and underneath this console log statement i'll paste this in and we will write a file but it's just a little different i can change just a little bit of the copy and paste so instead of read file we've got write file and this path here will change the file
name because we're going to write a new file this will be reply dot text now we don't have to specify the utf-8 that is by default now we will have a callback and the callback will only have an error we're not reading data we're writing it so if we have an error we'll throw the error in here instead of data we can just say operation complete or more specifically
let's say write complete and we can save this and now let's go ahead and run this code and we got an error oh and that's because i forgot to specify exactly what we're writing to the file i'll just say nice to meet you and then we have our callback had things out of order so again path name content we're going to put in the file
and then the callback with that saved let's run it and there we go we've got hello hi my name is dave and then the right is complete let's look at the reply file we created and it says nice to meet you i'm going to copy all of this right file operation down one and then we can change it to append file which is updating a file adding more content to it
we'll go ahead and create a different file here and that is to show you that a pin file will go ahead and create a file as well if it doesn't exist so let's just call this test and we'll say testing text or something like that here and other than that it is basically the same syntax as right we'll save this and go ahead and run the code again
and we got write complete and append complete and then notice due to the asynchronous fashion of nodejs the read completed last this time and we have hi my name is dave after the other operations completed in the file tree we now have our test file as well and it has the testing text in so append file worth noting that it will modify an existing file and can append content to it but will also
it will create a file if it doesn't exist so due to the asynchronous nature of node.js and these different methods we're calling here from read file write file and append file if we wanted to modify the file that we created such as reply it would be better to put a pen file i'm going to cut this and put it inside of the callback
of the write file operation that way it would definitely create the file and then we would be ready to append to it instead of wondering if we would possibly create the file first with append and then write over it with write file so here let's change this to reply dot text and it says nice to meet you let's give a couple of line breaks
and then say yes it is and if we save that we'll go ahead and delete the test that we're no longer using and our reply because we will create it once again so we've got reply and then we will modify reply with append and it's inside of the callback of write file call node index in the terminal
hello write complete then we got the read operation completed and then the append completed so if we look at reply now it says nice to meet you and yes it is in the order we expected now as you might imagine if we wanted to do something to this file after we added more content and we wanted to make sure it happened in the order it needed to such as renaming the file
then that would need to be in the callback of the append file i'm going to go ahead and copy a pen file and then inside of the callback i'm going to paste it and change this append here to rename so we can rename the file and we're grabbing the reply file and then we're going to replace this parameter with
what we're going to name it and we'll just call this new reply and then of course it has a callback with an error as well and then we can put rename complete now let's go ahead and call all of this in the terminal with node index and it does happen in the order we expect we got write append and rename and you can see this time the read operation
completed before the others once again asynchronous so we don't know which will happen before the next but in this way we're controlling it because we're putting the append inside the callback of the right and we're putting the rename inside the callback of the append let's go ahead and look at our new reply and it does have the content we expected now if you've worked with javascript for a while at this point you would be saying yes this is nice dave but this is
starting to look like what is called call back hell and yes it is because we're putting one inside the next inside the next and they're all inside the callbacks of the other so we are controlling the flow but at the same time we're able to avoid this in vanilla javascript using async await and now we can look at how to avoid it in