Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What is NPM?

Node JS is a powerful environment for the purpose of developing applications. This environment today has become more powerful due to the high availability of various features. One such feature of this environment is known as Node Package Manager. This feature is shortly known as NPM. As the name suggests this platform has a rich set of libraries. These libraries contain the code for each and every logic of an application. So the developers make use of these libraries, to develop the application. Moreover, by utilizing these libraries, the developers do concentrate on the actual code rather than the package dependencies. Like other languages for the application development, this platform also has the libraries under npm. And as mentioned earlier, this npm is the most important and interesting feature for the developers to develop the application. So today, in this article, Let’s have a look regarding npm

So let us initially know,

What is npm?

NPM stands for Node Package Manager. These packages sometimes referred to as the Modules. As the name suggests, this npm is responsible for managing all this package manager is responsible for managing all the modules/packages.  Forn node0.6.0 this npm latest version has been added the default in the node installation.  Some people define this npm as the package manager for the Node JavaScript platform.  This NPM is responsible for placing all the node modules in a single place. So that node can find the packages and allocates the dependent dependencies.

Moreover, this npm is capable of supporting a wide variety of use cases. And this npm is responsible for performing two things. At first, it is an online repository, for publishing the open-source projects. Secondly, it is a command-line utility for interacting with the repository. Besides, this npm is responsible for package installation, version management as well as dependency management. A different variety of nodejs applications were published every day.  And you people can search these applications on http://npms.org. And once the package is installed, we can install the required packages with the single-line command. Moreover, these modules were the reusable components and enhances the usage of the modules.

NodeJS Modules:

In NPM, these packages were further divided into the following types as follows. They are:

1) Core Module:

Since the nodejs is the lightweight framework,  this core module bundles is capable of handling only the absolute minimum functionalities. These modules gets generated, when the nodejs start execution. These core modules were automatically compiled into its binary distribution and loads the module when the node.js starts. Hence you people just need to import these modules to execute the code. Furthermore, these core modules were further divided into the following types:

a) http:  This module contains the classes, methods as well as the events to create the NodeJS HTTP server

b)URL: Contains the methods for the URL resolution and parsing in the node

c) query string: Contains the methods to deal with the query string of the node.

d) fs: Contains methods to deal with the file paths

e)util: Contains utility functions to deal with the programmers.

Moreover, in order to use this function, you just need to import it using the require() as below:

var module = require('module_name');

As per the above syntax, specify the module in the require() function.  This require function will return the object, property (or) any other javascript function, depending on the specific module that it returns.  And the below example demonstrates the utilization of Node.js http module to create the webserver.

var http = require('http');

var server = http.createServer(function(req, res){

  //write code here

});

server.listen(5000); 

In the above example function return the object, because http modules return the module of the object. And you can use its properties as well as the methods using the dot function. Hence like this, you people  create and utilize the nodejs core module application.

Get more practical knowledge of creating the nodejs application at NodeJS Online Training.
Node JS Local module:

These modules were created locally in your nodejs application. And these modules include the different functionalities of the application in a separate file as well as the folders.  And you people can package and distribute via NPM.  Moreover, these files/folders will be utilized by the communities as per the requirement.

Let us get more information on this through the code below.

This code contains the information that warns (or) logs the error to the console. Usually, the nodejs module is stored in the separate JAVA Script file. Hence create the JS file with the following code.

JS File Code:
var log = {
            info: function (info) { 
                console.log('Info: ' + info);
            },
            warning:function (warning) { 
                console.log('Warning: ' + warning);
            },
            error:function (error) { 
                console.log('Error: ' + error);
            }
    };

module.exports = log
 As in the core module, in local modules, we must create the utilize the require().  And you need to specify the path of the JAVA Script module. Hence the following code explain the utilization of logging module in log.js
var myLogModule = require('./Log.js');

myLogModule.info('Node.js started');

In the above code, it initially uses the log module. Initially, it loads the logging module using the require function and path location.  And logging module usually contains the log.js file in the root folder. Hence you need to specify the path ‘./log.js’ in the require function.  And the ‘.’ denotes the root folder. And this requires () returns the log object.  This is because this module exposes the object in log.js using the module exports. Hence you people can use this module as an object and call any of the function using ‘.’ notation. And now run the above example using the command prompt in windows as shown below

C:\> node app.js
Info: Node.js started

Hence like this, you people create the local module using the module.exports function.

And now lets move on to

External Module:

Even though NPM provides the various module for the development of the application, these libraries may (or) may not meed the requirement of the application. And there are some constraints,  where the developer needs to import the external libraries to the node JS platform. Hence this platform allows the developers to import the libraries to the application.  In node, export is the special object that is included in the every JS file in the Node.Js application by default. Usually, this module is a variable that represents the current module. And export is an object that is exposed as a module.  Hence the data that is allocated to the module. exports (or) export will be automatically taken as a module. Hence let us see how to expose different types as modules.

Export Literals:

As mentioned earlier, this export is an object. Hence it exposes whatever you assigned to it as a module.  For instance, when you assign the string literal,  then it will expose that string literal as a module.  The following example exposes the message as the module.

module.exports = 'Hello world';

//or

exports = 'Hello world';

And also you can import this module as shown below

var msg = require('./Messages.js');

console.log(msg);

Now run the above example and see the output

C:\> node app.js
Hello World

And you must use’/’  as the part of the root folder to import the module.

Export:

Export is an object which is capable of adding the methods(or) the properties to it. The following example exposes an object with the string property in Message.js file

exports.SimpleMessage = 'Hello world';
//or
module.exports.SimpleMessage = 'Hello world';

In the above code, we have attached the simple message to exports the object. And now you can import and use this module as below:

var msg = require('./Messages.js');
console.log(msg.SimpleMessage);

In the above example, the require() will return an object and assign it to the message variable. Hence now, you can uses msg.SimpleMessage.

And now, run the above example by writing the node app.js in the command prompt and see the output as below

C:\> node app.js
Hello World

And in the same way, we can expose an object with the function.  Moreover, we people can perform various kinds of operation using node.js. And as mentioned above, this npm is the best feature of this nodejs platform.  And people can get  the practical knowledge on npm version  form real time experts with real-world examples at Nodejs training

The post What is NPM? appeared first on Online Courses | Online IT Certification Training | OnlineITGuru.



This post first appeared on Java Online Course | Java Certification Course, please read the originial post: here

Share the post

What is NPM?

×

Subscribe to Java Online Course | Java Certification Course

Get updates delivered right to your inbox!

Thank you for your subscription

×