Don't reinvent the wheel. We're creating a great library here, but we don't have to write every line of code. Learn how to depend on other libraries while creating your own in this lesson.
With our MPM configured and our PGS JSON created, we are ready to start writing this library.
We need to create our main file for our library. We're going to create a source/index.js. I file in a source directory called index.js. This is where the main export for our library will live. When we say module to exports as common js syntax, this is the object that will be exported when people require our module.
We're going to have two public API points on our module. First, we'll have all and then random. These properties will be used to interact with our module. The idea is that we want to have our Star Wars names exported as part of the all, and we'll have a function that you can call to get a random Star Wars name.
For random we need to create a function that will get a random item from this array. This is a solved problem. Instead of taking some time to figure out how to write this properly and then making sure that I cover it with tests properly, I'm going to use a micro library that has already done this.
We're going to say MPM install--save to save this dependency to our package JSON. Then the dependency is called "unique random array." Once that's installed it will be added as a note module right here. It will be added to our package JSON as well because we have the --save there. It's added as a dependency.
Because we earlier configured our MPM to use save exact by default, we are saving the exact version here. We won't be surprised when somebody accidentally releases a breaking change. Let's go ahead and use this module. We'll say "/unique random array equals require unique random array."
All that we need to do is say unique random array and pass our array. That will return us a function that we can call to get a random item from that array. If we want to manually test this really quick we can go into the node ripple. We can say var lib equals require source/index.js. Now we can say lib.all and there are all of our random names.
We can say lib.random and invoke that over and over again, and we get a random Star Wars name every time. It looks like from our manual testing that our library is working like we want it to. We're ready to commit these changes that we have and push them up to GitHub.
Before we do that, let's go ahead and review what we did. First, we created our index.js file that had our library in it. We moved the Star Wars names to the source directory right next to it. We brought that in. We exported it an all object, and this is what's going to be used when people require our module.
We also created a random property. We're using a third party library to accomplish this. We installed this by saying MPM install--save unique random array. Just as a pro tip, there's a shortcut for this. We can say MPMI-S and that is the exact same as MPM install--save.
You can also install multiple dependencies in a single command by listing the dependencies here. By adding save, it's saved our dependencies to our package JSON. We have configured our MPM to save exact by default, it's saved our dependency with the exact version, rather than having a caret or a tilde for our version.
This protects us from surprises when somebody accidentally pushes a breaking change or does an honor sendver. That is how you create your first library.