‫In this lecture I want to show you
‫a couple of different things involving modules.
‫And I want to start by basically proving to you
‫that Node does in fact wrap the code in our modules
‫into a wrapper function,
‫like I showed you in the last lecture.
‫So let's create a new file again, module.js,
‫and that's modules, like this.
‫Okay, and what I'm gonna do here
‫is to log to the console the arguments, okay?
‫So arguments is an array in JavaScript,
‫and this array contains all the values
‫that were passed into a function.
‫So when I log this arguments array to the console,
‫if we actually see some values there, well,
‫then it means that we're really in a function, okay?
‫So let's run modules.
‫And indeed, here we have the code in our arguments.
‫And so let's remember the five arguments
‫of the wrapper function.
‫The first one is the export, so this one,
‫which is currently empty
‫because we're not exporting anything.
‫The second one is the require function,
‫so we can see that here,
‫we have the require function indeed.
‫Then the third one is called module, okay.
‫And in module we have then module exports,
‫which again we talked about in the last lecture.
‫And what do we have here is not that important, again,
‫I just wanted to show you
‫that we're actually in a function right now,
‫so that all this code that we have in this module
‫is indeed wrapped into this wrapper function, okay?
‫Then number three and number four are the filename
‫and the directory name.
‫And indeed, this is the name of the module
‫that we're currently in, so modules.js.
‫And then here we have the directory name.
‫So I'm on my desktop,
‫and then in this now Node works folder.
‫So similar to this one here,
‫but then this one has slash the module name, okay?
‫So that proves us that, indeed,
‫all the code inside a module is wrapped,
‫and that we have access to all these variables,
‫so these arguments, okay?
‫So I just find this an interesting experiment
‫that we can do.
‫And we can actually do another cool thing.
‫So to actually show you the wrapper function,
‫we can require the module module, okay.
‫So there's a module called module,
‫which we actually never used but internally Node uses it.
‫And in there we have the wrapper.
‫And so that is actually this property
‫is actually the wrapper function.
‫So we can now take a look at that.
‫And where is that, ah, yeah.
‫So here, here it is actually.
‫So this is the wrapper function.
‫And so indeed you see export, require,
‫module, filename and directory name,
‫and then the function body.
‫And so this is what Node internally use,
‫so basically this template here, it uses it,
‫and then fills up the body of this function with our code.
‫Okay, so again, just an interesting experiment here.
‫Now the most important thing that I wanna show you here
‫is how we can export and import data
‫from one module into the other.
‫And so let's start by creating a new module here,
‫in which I'm gonna create a calculator,
‫a bit like I mentioned in the last video.
‫So let's just call this test-module-1,
‫because we're gonna have a couple of them.
‫And here I'm gonna create a calculator class.
‫So again, this is the ES6 syntax
‫of writing classes in JavaScript.
‫So let's add a couple of methods here, so add a and b,
‫will of course return a plus b,
‫so very simple stuff, multiply a, b,
‫will return a times b, and then just,
‫let's just do divide as well,
‫just for the sake of completeness here, a, b, like this.
‫Give it a save, it nicely formats it,
‫and now we can do module.exports,
‫just like we learned in the last video,
‫and we're exporting our calculator, okay?
‫So again, we use module.exports
‫when we want to export one single value.
‫And in this case our single value here
‫is the calculator class.
‫So module.exports is exactly what is returned
‫from one module, so whatever we put there, well,
‫then gets exported automatically, right?
‫We can then save the exported value
‫to a variable when importing it, so let's do that now.
‫And now here we can actually give it any name that we want.
‫So the name that we are exporting on the other side,
‫so in this other module, doesn't matter.
‫We can call it anything we want here.
‫So in that sense it's just like a normal function return,
‫right, so we can always return any variable,
‫but then call it something else
‫when we basically save the result of a function
‫to a variable, right?
‫So since it's our own module,
‫we have to use the dot slash, so what I did just here,
‫and then test-module-1.
‫And the JS, remember, we can just drop that,
‫it's not necessary, okay?
‫And so this C variable here, which is uppercase
‫because for class we usually always use uppercase names.
‫And remember, we are actually exporting a class here.
‫Okay, so we can now use that class to do some calculations.
‫So first of all let's create a new calculator,
‫calculator one, because later on
‫we're gonna have another one, okay?
‫And so now we create a new instance
‫of a calculator like this,
‫and this should be nothing new for you at this point.
‫And then let's log through console
‫the result of actually using this.
‫So calc1.add, and let's say two and five.
‫Give it a save, and actually let's comment out these two.
‫Save it again, and then Node modules,
‫and here we have the result, seven.
‫So that's two plus five, so it works, right?
‫And here in our test module,
‫we could actually do it in a more elegant way.
‫So we could assign this class here directly
‫to module.exports, right?
‫So right now we have a class declaration,
‫so that's a little bit like a function declaration,
‫where we say function calculator, and then this,
‫and here we say class, okay?
‫But we could do it with a class expression as well.
‫And so like this we simply don't use
‫the calculator name anyway.
‫So copy it, comment it out.
‫Okay, so just like this.
‫So we just say that this is a class,
‫and then assign it to a variable.
‫And so that's now an expression.
‫So let's run this again,
‫and we see of course our seven again, okay?
‫So again, this is just to actually immediately assign
‫the value that we want to module.exports,
‫and that's gonna save us then some lines of code,
‫and probably make our code look a bit better.
‫Okay, so that's how we export stuff with module.exports,
‫and now let's see how and when we can use
‫the exports shorthand that I also mentioned
‫in the previous video.
‫So let's just create a simple comment here, module.exports.
‫Then here just exports.
‫So a new module test, and again, module-2.js.
‫And so the alternative for doing module.exports
‫is to add properties to the exports object itself.
‫So we could do it like this,
‫and using again the calculator example from before,
‫we could do it like this.
‫So we can simply add properties to the exports object.
‫So a and b will return a plus b,
‫exports.multiply a, b will return a times b,
‫and now let's just duplicate
‫just for the sake of completeness again, and okay.
‫So we created basically three anonymous functions here,
‫these three, and assigned them
‫to three properties of exports.
‫And so now when we export this module on the other side,
‫so in modules.js, we will get basically access
‫to this exports object.
‫So let me show that to you.
‫So let's say const calc2 is require.
‫Again, don't forget the dot and slash,
‫because it is our own module.
‫Okay, and so now this calc2 here is the exports object.
‫Okay, so let me show that to you.
‫Console.log, calc2.add, two and five.
‫So that should give us the exact same result as before.
‫And indeed, here is our seven, just like before.
‫And if we did multiply,
‫well then that should be of course 10.
‫And so yeah, we have seven here from the addition here,
‫and then this multiply gives us 10.
‫So I hope that this really makes a difference
‫between module.exports and exports clear.
‫And so again, when we're using simply exports,
‫we can add stuff to this object, so basically properties,
‫and then when we import that,
‫so when we require this module,
‫the result that we're gonna get
‫is an object containing all these properties.
‫And since we're getting an object,
‫we can use the power of ES6 destructuring
‫to do some cool magic here, basically.
‫So let me get rid of this one, or actually duplicate it,
‫comment out this one, and so we can use destructuring
‫to destructure the object that we get, okay?
‫And it works like this, so we use the curly braces,
‫and then we simply create variable names
‫for the properties in that object.
‫So again, let's use add, multiply and divide.
‫So that are the names that we actually have
‫on this exports object.
‫So add, multiply and divide,
‫and these actually have to be the exact same name
‫as in the original object, okay?
‫Now the thing that this will do
‫is that basically it will create a variable
‫called simply multiply.
‫And so indeed, here is our result.
‫And we can actually only import the ones that we want.
‫So let's say we only want add and multiply, okay?
‫And so we will only get access to these two,
‫instead of importing everything from the module.
‫And this is quite a common pattern
‫that you will see used in Node,
‫and we will do that throughout the project as well.
‫Okay, so in this video we talked
‫about the wrapper function here,
‫then we talked about exporting and importing
‫using module.exports and the exports shorthand.
‫And now finally, just to finish,
‫let's talk about caching very quickly.
‫Okay, so let's create test-module-3 here.
‫And what I'm gonna do here is to create a console.log,
‫so basically some top-level code inside this module.
‫Hello from the module, okay,
‫and then I also want to export a function.
‫Just one single function, and so I'm saying module.exports
‫equals function without any arguments,
‫one that will simply log to the console:
‫log this beautiful text.
‫Just some small emoji here to make it pop a little bit.
‫Okay, so let's now require this test module.
‫Test-module-3, and I'm not saving it to any variable.
‫Instead I'm going to call the function right away
‫without saving it to a variable.
‫So we could of course do that,
‫then we would have the function in a variable,
‫and we could then call it.
‫But this is the exact same thing,
‫because this will return this function that we defined,
‫so this one here, right?
‫It will return this because we are using module.exports,
‫and then right away we call this function here, okay?
‫And so actually let's do that three time.
‫And keeping in mind that we have caching in Node.js modules,
‫what do you think will happen when we run this code?
‫Okay, so let's run this and see what's gonna happen.
‫Here we have the result, hello from the module,
‫and then three times log this beautiful text.
‫So we have this logging here three times, well,
‫because we called the same function three times.
‫But we have hello from the module only once, okay?
‫And that is because of caching.
‫So technically this module was only loaded once,
‫and so the code inside of it was also executed once only.
‫And so that's why this line of code here,
‫this logging was only run once, okay?
‫And so these other two loggings here, well,
‫they came from cache, so they were stored somewhere
‫in the Node's processes cache.
‫And once we called the function here for the second time,
‫it was simply retrieved from there,
‫instead of loading the module again, okay?
‫So I'm sure that makes a lot of sense to you.
‫And so that was our lecture, actually.
‫If you have any questions, you can of course, as always,
‫post them to the Q&A and you will get help there.
‫So that finishes this entire section.
‫I know it was quite of a ride until we got to this point,
‫so a lot of stuff to take in.
‫And if you did correct everything 100%,
‫don't worry all too much about it,
‫because throughout the course,
‫most of the stuff will become clear eventually, okay?
‫So don't worry, just keep moving on in the course,
‫and I'll see you in the next section.