The other half of the pattern is module.exports, or the mechanism that we can use to make our modules consumable. Let’s go into our files. Under the Start folder, you’ll notice BenFranklin.js. Let’s go ahead and open that file up. This is the file that we built in the last lesson where we created a person object and then used that person object on line 10 to create an instance of Ben Franklin.
Let’s take this person object and put it in it’s own module. I’m going to grab everything from line eight all the way up to line one, and I will cut this code. We can save this file for now. Now I’m going to go ahead and create a new file. So I will go to File and select New File. Here we are at the Ben Franklin level. I’m going to go ahead and add a new folder called Lib from my library, and inside of my Lib folder I’m going to create Person.js. We’ll go ahead and save that file.
I will go ahead and save this file. Let’s go ahead and load it inside of our BenFranklin.js file. Here on line three, we are using a person constructor, but above line three we do not have the person constructor, we need to require it. I need to create a variable for person, and I am going to require our module. When we’ve been requiring core modules, we simply require them by name. When we require A custom module in our file system, we actually have to put the path to that module. This module is in the library folder, the Lib folder, and it’s Person.js.
We can just say this.name, and this time we’ll do a little different character here just to show that this is different, well use a little arrow there. We will also add said, which is what George Washington says, that is passed to this function. We have added our person object to a separate module, and we have used it to create two instances of people. I’m going to go ahead and save this. The last thing I’m going to do is also emit a George Washington quote.
George will emit a speak event and George would say, “It is far better to be alone “than to be in bad company.” All right, so now we have used our person object to create two instances of people, but we’ve required our person object from a separate file. That means that we can use this person object over and over again in all of our files because of the common JS module pattern. Let’s go ahead and save this and go out into our terminal and let’s run our file.
When we do so, we see that Ben Franklin says, “You may delay but time will not,” but George Washington says, “It is far better “to be alone than to be in bad company.” We have reused our person object and created two instances of people, but we also have separated our person object out into it’s own file, which ultimately makes it easier to reuse.