Modules in Node.js
Tags: node, webdev, backend, npm
Published: 2021-02-04 19:38:45

Node like most programming languages uses modules to achieve the goal of separation of concerns, code re-usability and organisation of code into manageable chunks. Each file in Node.js is treated as a separate module. This means creating a module is as simple as creating a new .js file.

Node uses the CommonJS specification for its module system. Modules in Node.js fall in one of three categories which are; Built-in(native or core) modules, Third-party modules and Local modules. I am now going to talk about each type of module in slight detail.


> Modules are loaded into a Node.js program using the ``require`` function. More on this later. Also note that anything that we define in our module (i.e. in our .js file) remains limited to that module only, unless we want to expose it to other parts of our program.

## Built-in Modules 

These are the native modules that come with Node.js. To use built-in modules, you need not install with npm or other package managers, all you have to do is to require(import) the native module you want to use. There are a lot of these built-in node modules. I will just state and describe a few of them that are often used for development of most applications.
- **fs** - used to handle file systems.
- **http or https** - for creating HTTP(S) servers
- **events** - used to handle events.
- **util** - used to handle utility functions e.g deprecate, inspect and format.
- **buffer** - used to handle binary data.
- **stream** - used to handle streaming data.
- **path** - provides utilities for working with file and directory paths.
To checkout the list of all the other Node.js core modules, check out the official documentation  [here](https://nodejs.org/api/index.html) 

## Third-party Modules

Third-party modules are modules that are usually installed from a package repo. They modules are commonly installed using npm or other package managers like yarn. We use them to accomplish or simplify any existing task. For example, to simplify our web API development we use ``express``, or to deal with date and time we use ``moment`` or to monitor changes and automatically restart your node server, we use ``nodemon``. Third-party modules are not only gotten or installed using package managers, modules can also be hosted on GitHub as well as private servers.
To install a third-party module locally in your project folder is as easy as running the following command:
```
npm install <package-name>
```
The command above installs the requested package or module into the node_modules folder in your project folder.

## Local Modules
These are the modules that we create for our own use. They are created locally in your Node.js application.

I think what has been said above is enough to get you started with node modules and ending this post without saying something on npm will be unfair as it is the largest online registry or repository for javascript packages. I am going to talk a little on npm and drop some few npm commands as well.

## npm (Node Package Manager)

npm is a package manager for Node.js packages or modules. It is the biggest single language code repository on Earth, isn't that amazing? This only means you can find a package for almost anything on npm's registry. It was developed by  Isaac Z. Schlueter and it is entirely written in javascript. Below is a list of some common npm commands.

- ``npm init`` - used to create a package.json file
- ``npm search <term>`` - used to search the npm registry for packages matching the provided search terms.
- ``npm install <package-name>@<version>`` - used to install a specific version of a package.
- ``npm install -g <package-name`` - used to install a package globally.

And there you have guys, this section brings us to the end of this post. As always if you enjoyed this piece please do well to like and share. I'll love to connect with you on social media. Here is my Twitter and linkedIn. Twitter  [@brandonbawe](https://twitter.com/flaacko_flaacko) LinkedIn  [Damue Brandon](https://www.linkedin.com/in/damue-brandon-bawe-73ba121a1/) . See you next time 👋🏾👋🏾.