Introduction to Node Modules

Introduction to Node Modules

Node modules are basically libraries that gives you extra functionalities that is not available on the V8 engine runtime. There modules from libraries / packages developed as part of core Node.js. We will examine those default libraries in detail here. There are also other libraries developed by other developers that are not as part of core Node.js.

Node Package Manager (NPM) Node package manager is a library built to help you download Node.js libraries from the global npm registry. Confusing?

So, there is a large registry containing Node.js packages developed by the core Node.js team and other developers. To be able to use packages from this registry you need something to download the packages and make them available with your project.

For example, to make Bootstrap CSS available in your html project, you usually would include a <link> tag, like this: <link rel="stylesheet" href="path/to/bootstrap.css"> That will allow you use bootstrap class in your project. This type of import is regarded as CDN.

NPM however, downloads the package into your project and make it available, when you use the keyword require within your code. We will look at this in more detail later.

If you have node installed on your computer, then, npm will also have been installed along with it.

npm does not only download packages, it also has a bunch of other functions. One important one is that it can help you start a new node project. See how below.

Starting your own node project To start a node project from scratch all you need is run the command npm init. What this command does is that it essential generates a JSON file for you, known as package.json. A package.json file typically carries the application name, version, creator and the dependencies in your project. Dependencies are the various libraries and packages that your app needs to run either in production or in development. Dependencies (packages) that are only useful for development purposes are created in the package.json file as 'dev-dependencies'.

Enough talk! Let's see some action!

Go into your terminal and type npm init and press enter. You will immediately enter an interactive session where the are requested to provide details about your project. Like this below,

Screenshot from 2021-04-05 22-51-13.png

Above, is a sample npm init response, I have filled the fields required. After filling all, you see the response about to write to path/to/package.json, then it shows the details of what will be written to package.json and asks if that is ok.

If we press enter. A package.json file will be created in that location specified. And here is what our package.json looks like.

Screenshot from 2021-04-05 22-57-28.png

Now, let's take a step further and import a package using npm and see what happens!

Importing Node Modules With NPM

Now, i have created a folder with the same name as what is in my package.json, and moves my package.json into that folder, so here is what my directory structure looks like.

Screenshot from 2021-04-05 23-03-36.png

Notice, I have a folder reskill-americans-demo, and a file package.json. The contents of the package.json were displayed above.

Let's say i want to import a package known as uuid into this project. Because i may need it later. We are not concerned with what the package does for now. We just want to import it from the npm registry, what do we do?

It's easy, we run the npm install uuid. And three significant things happen automatically!

  1. A new folder is created called node_modules.
  2. A new entry is added to the package.json called dependencies with the newly installed package - uuid - added to it.
  3. A new file is created called package-lock.json.

Let's see it!

Screenshot from 2021-04-05 23-09-48.png

The node_modules folder This is the folder that holds the actual code file for each package (dependencies) within our project. Note! The node_modules folder is only created the first time! Subsequent installations will only add more files to the same folder. Hence, that folder can get really large. When pushing files to github (or any other repository service), you make sure not to push the node_modules folder. To avoid doing that, you need to include a file in your project called .gitignore and within it include node_modules, this ensures that your node_modules do not get pushed along with your code to github.

For further exploration, you can open the node_modules folder to see what is actually there!

The package-lock.json This is a file that keeps more detailed information about the packages (dependencies) that you have in your project, this ensures consistency in the packages (dependencies) you have in your project. Most of the time, you shouldn't have to touch this file.

Notice the new dependencies entry in our package.json at the end of the file.

So what happens when you join a team of developers, and they have a node project already with a package.json, and you need to import (install) all the packages that already exists in the package.json as dependencies or dev-dependencies?

You run the command npm install, notice we didn't specify any package. This command will install all the packages it can find in the package.json file!

Now, let's install a package as a dev-dependency. That is, the package is only useful for development and should not be in our final user-ready app. (Not that the user would notice).

Installing a dev-dependency To do this we use the command, npm install dependencyName --save-dev.

Example:

Let's install nodemon. A development package that helps to refresh the server.

we will use the comma nd npm install nodemon --save-dev. Let's see it!

Screenshot from 2021-04-05 23-32-55.png

Now, we see an entry for devDependencies in the package.json and we see nodemon and its version 2.0.7.

That's how its done!

Subsequent classes we will see how to import these packages and actually work with them in code!

See you in the next one!