How to Import a Module in node.js



node.js

In this article, we show how to import a module in node.js.

A module is package that contains preprogrammed instructions that streamline coding, making coding much simpler and efficient.

Importing modules is absolutely when coding in most programming languages, including node.js

There are a few types of modules when working with node.js.

There are core modules in node.js, which are modules that are built into the programming language already. All you have to do is import it.

And there are third-party modules, which are modules that aren't built into node.js. They are completely external.

We will show how to import core, third-party, and your own custom modules in this article.

How to Import a Core Module

So the first and easiest module you will show how to import is a core module.

In node.js, these include modules such as fs module (file system), the events module, etc.

So let's show this in code below.



In the code above, we import the module, fs, which stands for file systme. It is a module that allows us to work with files on our system.

We import a module in node.js using the require() function. The require() function simply takes in the name of the module.

We then have to assign a constant variable, fs, to this require function, so that we have a reference to it in our code and can use it throughout our script to deal with files.

So this is the process for importing a core module in node.js.

How to Import a Third-Party Module

Next, we now discuss how to import a third-party module.

The most common third-party modules are npm modules.

The biggest place to find these third-party npm modules is on the website, npmjs.com.

Here you can search for the type of module you are looking for.

When you first download node.js, there are a few things you need to do to able to use npm modules.

First, open up the node.js software.

Then go to the terminal on the node.js software and run the following command below:



You should get an output such as the following below.

Initializing npm in node.js

When you initialize npm, a number of things will be asked for, such as the package name, version, description, entry point, test command, git repostiory, keywords, author, and license...you can simple click 'Enter' through all of these without specifying anything. The package manager will then be installed.

When you initialize npm, you're doing a few things. You're creating a package.json file, which keeps track of all npm modules that you install. You also create a package-lock.json file, which stores meta data about the npm package manager that you have installed such as the version you're using and where it was downloaded from.

Next, you have to make sure that you install the npm module that you want to use.

In our example, we will be using the validator npm module.

Therefore, you must go to the terminal of your node.js software and type in the following command.



Or



Both do the same thing of installing the validator npm module.

This is how the installation will look.

Installing an npm module in node.js

Once this is installed, you will see a message that the package is added. You will also see that in your node_modules foler, you will see files that pertain to this newly installed npm module, such as validator.js.

Now let's go on to using the validator module.

We will show how to import the module and then use it for a simple task.



The method for importing a third-party npm module is the same for importing a built-in core module.

The require() function is used, specifying the name of the module.

We then create a variable and set it equal to this require function.

This console.log statment will either output true or false. In this case, it outputs true because we entered in a valid email.

And this is how we can import either a core module or a third-party npm module in node.js.


Related Resources

How to Find the Version of node.js Installed on Your Computer



HTML Comment Box is loading comments...