Node.js Module system Part 2( note-taking app)

CG_Musta
6 min readAug 22, 2020

--

Export files and functions

In the first part of this blog, we explained how to export functions or variable from one file to another. Now we are going to start building our note app by using all we learned so far but before we need to explore the npm package how to install it and lead it in our application.

Create a file note.js, inside the file write a function “getNotes()” for now we need to export this function to be able to use it elsewhere so the return value will be random for the moment.

now we need to export the function as we learned previously

module.exports = getNotes

In app.js, we need to require/load the file, call the function and test that the message gets to print out in our console.

As you can see, we required the module/file and assigned the return value to a variable with the same name of the function created, but we could have used any other name. We then loaded the function and assigned to a variable “msg” and used the console.log() method to see the result on the terminal.

So fare we saw how to load a core node file, and we also learned how to load other files we created, now we need to understand how to load the npm module.

Importing the NPM modules

Now we need to understand how to use the module system to load the npm packages. There are an endless amount of npm packages that we can take advantages of in our application, for example, validating data like email or sending emails. These are core functionality and not specific to what your application does. Npm modules give us the chance to solve common problems so we can focus more on the features that make our app unique.

Before to use the npm packages we need first to initialize the npm module in our application, and we need to install all the modules that we need to us. In the terminal of the directory that you want to install the npm module into type:

npm init

This command is going to initialize the npm module in our application by creating a single configuration file that we can use to manage the dependency form the npm website that we want to use.

When you enter this command is going to ask you to confirm some information we need to stick with the convention format and hit entre for all the questions asked. The packages installed should look something like this:

the extension of the file is “.json” that stands for “JavaScript Object Notation” and look like a javascript object.

NPM Validation Package

This package will help us with any data validation. Let’s go on the main website, npmjs.com, and in the search bar write: “validator”.

We can find in this page the documentation on how to use it and on the right-hand side information about the package itself.

Without this package, you will have to write the validation entirely by your self, maintained and write test cases and keep up the update while the node is progressing over time. But since validation is not the main focus of your application this great npm package will make your life easier and give you the change to focus on the main core of your app and what makes it unique.

Of course, this does not mean that you won’t write code but for this will allow us to maintain and write it in a much more secure way. The validation package has all the functions and tools to validate emails, phone numbers, credit cards and other types of string information.

To install the package, we need to run the command:

npm install validator@13.1.1

or

npm i validator@13.1.1

Both ways to install the package are totally fine and works the same, and the number following the command is the latest version that you can find in the package in this case mine is 13.1.1 maybe while you are reading this blog the version number is different but it does not matter just use the one you find on the website.

When we run this command is going into the npm server to grab all the codes for that package and add them to our application. After running this command we can see some changes in our application.

First, a new “package-lock.json” file appeared and we have a new directory “node_modules” which contain all the dependency for the code we installed and if we opened it we should see the validator package we installed and inside should contain all the code for the validator package.

The package-loc.json list all the information about the packages we installed making npm faster and secure.

Both of this new file we have should never manually modified, npm will take care of this for us.

Now in our “package.json”, we added the new validator package under the dependencies property along with the version number.

Now we need to load the package in our file as we have seen before using the require() function and assigned the return value to a variable.

const validator = require("validator")

Now is time to test our package if it works fine, let’s grab from the documentation the function that validates the email “isEmail”. we need to call this function on the validator object we got when we loaded the package in our file. Inside we will pass an email which includes all the parts required for an email to be valid, and print the result to the console.

validator.isEmail("mustafa@test.come")

In the console, if the validator works fine you should see “true” as a boolean value. Now is time to check with a non-valid email by removing the @ and see what’s happening, in this case, you should see in the console a boolean value of “false”. Great!! our validator works fine.

So far with part 1 and 2 of the blog we have learned the following:

  1. Load in core node modules, which are modules included with the node installation.
  2. Load in third party modules written by other developers
  3. Load in modules that we can create

Now with this knowledge, we can go ahead and start creating our note-application from the next blog.

--

--

No responses yet