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
requirefunction. 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.
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
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.
These are the modules that we create for our own use. They are created locally in your Node.js application.
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 LinkedIn Damue Brandon . See you next time 👋🏾👋🏾.