Before reading this article, I recommend my previous part:
- 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:
- 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
- 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
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:
- {
- "name": "My_Module",
- "version": "0.1.0",
- "description": "a simple node.js module",
- "dependencies" : {
- "express" : "latest"
- }
- }
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.
- {
- "name": "my_module",
- "version": "0.1.0",
- "dependencies" : {
- "express" : "latest"
- }
- }
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:
- Create a project folder named “demo”. This will be the root of the package.
- Inside that folder create a file named demo.js.
- 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:
- var Words = ["sad", "bad", "mad"];
- var addWord = [];
-
- function demo(inStr) {
- for (idx in Words) {
- inStr = inStr.replace(Words[idx], "****");
- }
- for (idx in addWord) {
- inStr = inStr.replace(addWord[idx], "****");
- }
- return inStr;
- }
-
- function add(word) {
- addWord.push(word);
- }
-
- function get() {
- return Words.concat(addWord);
- }
- exports.demo = demo;
- exports.add = add;
- exports.get = get;
package.json code:
- {
- "author": "NeErAj KuMaR",
- "name": "demo",
- "version": "0.1.1",
- "description": "demo words",
- "main": "demotext",
- "dependencies": {},
- "engines": {
- "node": "*"
- }
- }
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.
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:
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:
I hope you will understand this article about Node.js.