Nodejs – Packages and Modules: How to use it?

Let’s discuss the usage of Nodejs packages and modules and how it has been used in Nodejs projects.

Nodejs is blessed with its extensive package management system called Node Package Manager (NPM). Each time a Command Line Interface (CLI) for npm comes as an add-on with Nodejs installation, allowing developers to connect with packages locally on their machine.

Build and scale your eCommerce business via Headless with Node!!!

Nodejs comes with some plain JavaScript files that contain a bundle of codes for a specific purpose called “Modules.” The module pattern helps your code to navigate and works in a more accessible manner. To use such properties of a module, you must require it from a JavaScript file, the same as importing a package in a Java class.

Nodejs Packages and Modules: Things to know

Nodejs Packages and Modules:

There are three modules available in Nodejs: user-defined modules, core modules, and external node_modules.

In the require function, if the module name passed prefixed with ‘./’ or ‘../’ or ‘/,’ then it is assumed to be a file-based module from the package, and then the File is loaded.

Then we look for core Nodejs modules with the same name, for example, util the call required (‘util’).

If no core module has the exact name match found, we look for a node_module in the function called util.

Read: What is the Node version manager(NVM), and how to install it?

Nodejs Packages Location

All the packages and modules in Nodejs are stored in a subdirectory within your current directory, named as 

node_modules

To determine the location, use the following command,

npm root

To view all the installed modules, use the following command,

npm ls

You can verify the command module once after installing it, using the same command as above,

$ npm ls

/npm-test

 |

 [email protected]

     |

     [email protected]

The above code indicates that the commander module got installed. You can understand from the tree structure that the commander depends on the keypress module.

All the required modules in Nodejs automatically get installed by npm, recognizing its dependency.

Check for your installed Nodejs modules just by browsing the node_modules subdirectory.

Scanning for node_modules

Nodejs scans for node_modules in a file system; a file /home/myUser/project/myData.js has a require call require(‘myModule’), then it will be in the following order,

/home/myUser/project/node_modules/myModule.js 

/home/myUser/node_modules/myModule.js 

/home/node_modules/myModule.js 

/node_modules/myModule.js 

Nodejs looks for node_modules/myModule.js in the current folder and every parent folder until it reaches the file system’s path for the existing File.

The following code in c:/welcome/myData.js loads a module node_modules/myModule.js.

var myModule = require('myModule'); 

myModule(); // welcome node_modules! 

File under c:/welcome/node_modules/myModule.js

module.exports = function () {

    console.log('welcome node_modules!'); 

Except for scanning the file system to load the JavaScript file, all other behaviors are similar between the file-based modules and node_modules.

Folder-Based Nodejs Modules

Nodejs will do search performance to locate an index.js file in that folder and return that as the module file.

We illustrated an example to show you to do Implicit Loading of index.js from a Folder.

File under bar/bar1.js

module.exports = function () {

    console.log('bar1 was called'); 

Then, File under bar/bar2.js

module.exports = function () {

   console.log('bar2 was called'); 

File under bar/index.js

exports.bar1 = require('./bar1'); 

exports.bar2 = require('./bar2'); 

Here is the bar module that uses the File of myData.js.

var bar = require('./bar'); 

bar.bar1(); 

bar.bar2(); 

Note

We illustrated an example to show you how to change the folder-based module to node_modules,

You can change  the require call from require(‘./bar’) to require(‘bar’).

// node_modules/bar/bar1.js 

module.exports = function () {/*from www . webnexs. com*/

   console.log('bar1 was called'); 



// node_modules/bar/bar2.js 

module.exports = function () {

   console.log('bar2 was called'); 



// node_modules/bar/index.js 

exports.bar1 = require('./bar1'); 

exports.bar2 = require('./bar2'); 

// myData.js 

var  bar = require('bar'); // look for a node_modules module named bar 

bar.bar1(); 

bar.bar2();

Final words

In this blog, we discussed various scenarios to demonstrate how to use Nodejs packages and modules. And how Node Package Manager manages these packages.

Are you having any quires about Nodejs web development? Contact our Nodejs experts for more information flow.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *