‫So what I wanted to show you
‫in this last video about the project
‫is that we can actually create our own module
‫and export something from them like for example a function.
‫Then import this function into another module
‫and then use that function there.
‫Okay, and so that is what we're gonna do in this lecture.
‫So let's say that we actually had
‫a bunch of different JavaScript files
‫in which we used this replace template function.
‫So right now we're just using it here in index.js.
‫We use it twice and that is why we have a function,
‫but imagine if we wanted to use this function
‫in multiple files.
‫Okay, so what we can do is to create a new module
‫and export that function from it
‫and then import it back here.
‫So, the first thing that you need to know
‫is that in Nodejs
‫actually every single file is treated as a module.
‫And so this index.js here actually is also a module,
‫which in this case imports other modules
‫and particularly these three.
‫Let's now create a new folder here,
‫which I'm gonna call modules
‫and then in here
‫I'm gonna create a file
‫which will be our module called replaceTemplate.js.
‫Okay, so let's grab our function here.
‫Actually, cut it out from here
‫and simply paste it here.
‫All right, now how do we actually export this function
‫from this module?
‫Well, there are different ways of exporting something
‫from a module
‫and we're gonna talk in depth
‫about all of this in another section later on.
‫But for now
‫all we're gonna do is to use module.export.
‫So in each module,
‫we have access to a variable called module
‫and on there we can set the export's property.
‫And that we then set to whatever we wanna export.
‫In this case, that is just this function.
‫So we're gonna get rid of this name
‫and so basically
‫what we're gonna assign to this export property on module
‫is simply this anonymous function.
‫I said anonymous
‫because right now this function does not have a name.
‫And so it's just an anonymous function
‫to be assigned to this export property
‫on the module object.
‫Again, that is an object that we have access to
‫in each and every Nodejs module.
‫And again,
‫later on you will learn
‫how that actually happens behind the scenes.
‫But for now,
‫let's just use it
‫and don't worry about why it works this way.
‫So this exports this function from the module.
‫Let's just go to index.js and import it.
‫And imports usually always happen at the top of the file
‫and after the core modules.
‫First, we have the core modules
‫and then we have our own modules.
‫We will do require and then our own module,
‫which is dot...
‫And remember in the require function,
‫the dot actually means the current location of this module.
‫So remember when we talked about the dir name variable here,
‫which is usually the one that points to the module's folder.
‫But in this case in the require function,
‫remember there is an exception
‫where this dot actually points to the location
‫where this module is in.
‫And so right now,
‫that is this root folder here.
‫And so from here we go to modules
‫and then in there we have replaceTemplate.
‫And again,
‫just as with the other modules,
‫we don't need the .js.
‫Now that is gonna return what we exported here
‫with module.exports.
‫What we have here, this code, this function,
‫is what will be exported from this require function call.
‫And so we can now save that into any variable
‫and give it a name that we want.
‫So we could just call it X if we wanted
‫or really whatever we wanted.
‫But instead I am just giving it this name
‫that we already had here
‫so that I don't have to change my code here.
‫So replaceTemplate is the name
‫that I wanna give to this variable
‫which is now this function.
‫And just like this,
‫we exported something from one module
‫and imported it into another one.
‫And so now if we restart our server,
‫everything should still be working.
‫But it actually does not.
‫We have some kind of error here.
‫And it says it cannot find the module.
‫And that's because I was stupid enough
‫to call this folder here modules.js.
‫I am sorry for that.
‫This one is of course
‫only supposed to be called modules.
‫And that's why it wasn't giving me the auto-complete here.
‫I was wondering why that was.
‫And now we know.
‫If it doesn't work for you,
‫just change the name here of this folder
‫and then everything should match and work as it should.
‫Let's reload and it still replaces everything as it should.
‫The same here of course.
‫And so yeah, we are now done with this project.
‫Congratulations, well done.
‫You are well on your way of learning Nodejs.
‫Now in the rest of the section
‫we're gonna talk a little bit
‫about the node package manager or npm.
‫And so we're gonna keep working a little bit
‫with this project
‫but not really adding any functionality.
‫Anyway, see you in the next video
‫where we're gonna start talking about npm.