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 application development, this platform also has 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 at 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 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 reusable components and enhance 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 a lightweight framework, this core module bundle is capable of handling only the absolute minimum functionalities. These modules get generated, when the nodejs start execution. These core modules were automatically compiled into their binary distribution and load 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 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 requires 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 the 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.
||{"title":"Master in NodeJS", "subTitle":"NodeJS Certification Training by ITGURU's", "btnTitle":"View Details","url":"https://onlineitguru.com/nodejs-course.html","boxType":"demo","videoId":"-EAMZHHfRP4"}||
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 a 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
var myLogModule = require('./Log.js');
myLogModule.info('Node.js started');
The above code 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 functions 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 let's move on to
External Module:
Even though NPM provides various modules for the development of the application, these libraries may (or) may not meet 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 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';
//orexports = '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 the Message.js file
exports.SimpleMessage = 'Hello world';
//ormodule.exports.SimpleMessage = 'Hello world';
In the above code, we have attached a 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
Benefits of using NPM
The following are the important benefits and uses of NPM within Node.Js
It is useful to manage the local dependencies of various project tools.
Also, it is helpful in managing globally-installed project tools.
The NPM easily manages several versions of code and code dependencies.
Through NPM, users can easily download standalone tools you can use immediately.
Moreover, NPM offers package-lock.json that presents all dependencies of the project.
Package-lock.json - NPM
This package stores file produced automatically when a user requests package updates (add/edit/delete, etc). Moreover, this file displays all details regarding the package within a tree format. This file defines the actual dependency versions used within the NPM JavaScript project. Hence, the package-lock.json is a type of ingredient table. This file is produced by the npm install command.
Creating package.json file in Node.Js
The package.json file within any Node.js application supports the configurations needed for the application. It also includes the list of various NPM packages that are needed by the application. If a user starts a Node.js app with an empty folder, then he can add the package.json file to the project with the help of the below command:
> npm init
The above command asks the user for a set of inputs to be provided. Once the user provides all the details, it displays a preview of the file. Later, if the user accepts, it builds the package.json file with the details provided.
||{"title":"Master in NodeJS", "subTitle":"NodeJS Certification Training by ITGURU's", "btnTitle":"View Details","url":"https://onlineitguru.com/nodejs-course.html","boxType":"reg"}||
Moreover, there are some directives or fields of the Package.json file, such as;
name: The name field is a mandatory one here. The package registers with this name and this name only the clients can use to install the package. It has to take into note that “Name” cannot include spaces, cannot begin with an underscore or a period, cannot include the words node or, js. And also cannot be the same as a library module. Moreover, the name has to be unique and it also helps to search the package within the registry.
version: This is also a mandatory field within the package. It specifies the version of the JSON package in NPM. Moreover, it has a default value assigned by the npm init command is 0.0.0. Hence, this version value must be modified before every sequential deployment to the registry.
dependencies: There is a list of packages needed by the existing package in NPM. Also, the package uses these different packages to attain its functionality for long use.
devDependencies: This list of packages needed while developing the existing package. It has to take care that these should not be used within the functionality of the package.
Optional dependencies: the operational dependencies list of packages that may be the package needs.
Furthermore, there are engine, script, etc. fields in this regard.
Global Packages
There are many NPM packages in NodeJS like bower, gulp, grunt, etc. Also, there are several others that need global execution. They can be able to run them using the command prompt from any location. The major difference between the installing package command locally and globally is the flag (-g) for installing it globally. Moreover, these packages are easy to update and delete using the same set of commands. Some of them we have discussed above in this chapter. Here, a user needs to pass the flag to specify that the package is global and not local.
Conclusion
And in the same way, we can expose an object with the function. Moreover, we people can perform various kinds of operations 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 from real-time experts with real-world examples at Nodejs training