Nodejs – Packages and Modules: How to use it

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

Nodejs 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 which allows developers to connect with packages locally on their machine.

Basically 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 an easier manner. To use such properties of a module, you have to require it from JavaScript file the same as importing a package in a Java class.

Nodejs – Packages and Modules

node-modules

There are three kinds of 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 is called util.

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

Package Locations

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

 |

 +--commander@1.3.2

     |

     +--keypress@0.1.0

The above code indicates that the commander module got installed. From the tree structure, you can understand 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 path of the file system for the current 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 the scanning of 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 went through various scenarios to demonstrate how to use Nodejs packages and modules. And how these packages are managed by Node Package Manager.

Having any quires about Nodejs web development? Contact our Nodejs experts for more information flow.

Leave a Reply

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

Facebook
Twitter
INSTAGRAM
LinkedIn