Node Package Manager And Node Package Module In Node.JS

Before reading this article, I recommend my previous part:

  1. Installation And Working Of Node.js

1. Node Package Manager:

What is Node Package manager: The Node Package Manager is a Command line utility. It allows you to find, install, remove, publish and do a lot of other things related to Node Packaged Modules. The Node package Manger provides a link between the Node Package Registry and development environment. Node Package Manager is to list some of the Command line options.

Option Description Example
Search Finds Module packages in the repository npm search express
Install Installs a package using a package.json npm install
Install -g Install a package in a globally accessible location npm install express –g
remove Removes a module npm remove express
pack Packages the module defined by the package.json file into a .tgz file npm pack
view Displays module details npm view express
publish Publish the moduledefined by a package.json file to the registry npm publish
unpublish Unpublishes a module you have published npm unpublish MyModule
owner Allows you to add, remove,and list owners of a package in the repository npm add neeraj MyModule
npm rm neeraj MyModule
npm Is MyModule

Some examples of commands are shown below:

  1. Searching for Node Packaged Modules: you can search for module in the Node Package Registry directly from the command prompt by using the npm search <search string> command.

    npm search openssl

  2. Installing Node Packaged Modules: To use a Node Module in your application, it must first be installed where Bode can find it. To install a Node module ,use the npm install <module_name> command to download the Node module to your development environment and places it in the node_modules folder,where the instll command run. For example ,the following command installs the express module:

    npm install express

    npm install express

    The output of the npm install command displays the HTTP requests to download the necessary files that was installed with the module.

    But in my computer express is already installed, so this image doesn’t show hierarchy of files.

    package.json Structure: All Node Modules must include a package.json file in their root directory.package .json is a simple JSON text file that defines a module, including dependencies. The package.json file can contain a number of different directives to tell the Node Package Manager how to handle module.

    The following is an example of a package.json file with a name, version, description and dependencies:
    1. {  
    2.    "name""My_Module",  
    3.    "version""0.1.0",  
    4.    "description""a simple node.js module",  
    5.    "dependencies" : {  
    6.       "express" : "latest"  
    7.    }  
    8. }
    The only required directives in the package.json file are name and version.

    A great way to use package.json files is to automatically download and install the dependencies for your Node.js app. All you need to do is create a package.json file in the root of your project code and add the necessary dependencies to it. For example, the following package.json will require the express module as a dependency.
    1. {  
    2.     "name""my_module",  
    3.     "version""0.1.0",  
    4.     "dependencies" : {  
    5.         "express"   :  "latest"  
    6.     }  
    7. }
    Then you run the following command from root of your package and the express module will automatically be installed.

    npm install

Creating a Node.js Packaged Module

To create a Node.js Packaged Module you need to create the functionality in JavaScript, define the package using a package.json file and then either publish it to the registry or package it for local use.

The following steps take you through the process of building a Node.js Packaged Module using an example:

  1. Create a project folder named “demo”. This will be the root of the package.

  2. Inside that folder create a file named demo.js.

  3. Add the code is just basic JavaScript functions demo(), add() and get(). The exports.demo is required for Node.js applications using this module to have access to the demo() function as well as the other two.

Also make package.json file:

Demo.js file code:

  1. var Words = ["sad""bad""mad"];  
  2. var addWord = [];  
  3.   
  4. function demo(inStr) {  
  5.     for (idx in Words) {  
  6.         inStr = inStr.replace(Words[idx], "****");  
  7.     }  
  8.     for (idx in addWord) {  
  9.         inStr = inStr.replace(addWord[idx], "****");  
  10.     }  
  11.     return inStr;  
  12. }  
  13.   
  14. function add(word) {  
  15.     addWord.push(word);  
  16. }  
  17.   
  18. function get() {  
  19.     return Words.concat(addWord);  
  20. }  
  21. exports.demo = demo;  
  22. exports.add = add;  
  23. exports.get = get;

package.json code:

  1. {  
  2.   "author""NeErAj KuMaR",  
  3.   "name""demo",  
  4.   "version""0.1.1",  
  5.   "description""demo words",  
  6.   "main""demotext",  
  7.   "dependencies": {},  
  8.   "engines": {  
  9.      "node""*"  
  10.    }  

Packaged

Next navigate to the “demo” folder in a console window and execute the following command to build a local package module:

npm pack

The npm pack command will create a “demo-0.1.1.tgz” file in the “demo” folder. This is your first Node.js Packaged Module.

Packaged Module

npm install demo

If you want to install it in any folder enter command.

Navigate to the folder and after that open command prompt then write code:

npm install <directory of .tgz file>+file name:

npm install demo-0.1.1.tgz according to my location.

Install package image

Install package image:

node program

If you want to get output from demo.js file, then create another .js file that load demo project. After that use methods of demo, I have creates program.js file that contain the following code:

1. var demo = require("demo");
2. console.log(demo.get());
3. console.log(demo.demo("Some very sad, bad and mad text."));
4. demo.add("gloomy");
5. console.log(demo.get());
6. console.log(demo.demo("A very gloomy day."));

See below demo:

Then Naviagte to the folder again in command prompt and write command:

node program.js

Here's the output:

output

I hope you will understand this article about Node.js.

Up Next
    Ebook Download
    View all
    Learn
    View all