Node Package Manager (NPM) provides two main functionalities −
NPM comes bundled with Node.js installable after v0.6.3 version. To verify the same, open console and type the following command and see the result –
If you are running an old version of NPM then it is quite easy to update it to the latest version. Just use the following command from root –
There is a simple syntax to install any Node.js module −
For example, following is the command to install a famous Node.js web framework module called express −
Now you can use this module in your js file as following −
by default, NPM installs any dependency in the nearby mode. here nearby mode refers to the package installation in node_modules directory lying within the folder wherein Node software is present. locally deployed packages are accessible via require() method. as an instance, while we hooked up the explicit module, it created a node_modules directory in the current directory where it installed the express module.
Alternatively, you can use npm ls command to list down all the locally installed modules.
Globally installed packages/dependencies are stored in system listing. Such dependencies can be used in CLI (Command Line Interface) function of any node.js but cannot be imported using require () in Node application directly. Now let's try putting in the explicit module using global installation.
This will produce a similar result but the module will be installed globally. Here, the first line shows the module version and the location where it is getting installed.
You can use the following command to check all the modules installed globally −
The package.json is present in the root directory of any Node application/module and is used to outline the residences of a package. Let’s open package.json of specific package found in node_modules/express/
Use the following command to uninstall a Node.js module.
Once NPM uninstalls the package, you can verify it by looking at the content of /node_modules/ directory or type the following command −
Update package.json and change the version of the dependency to be updated and run the following command.
Search a package name using NPM.
Creating a module requires package.json to be generated. Let's generate package.json using NPM, which will generate the basic skeleton of the package.json.
You will need to provide all the required information about your module. You can take help from the above-mentioned package.json file to understand the meanings of various information demanded. Once package.json is generated, use the following command to register yourself with NPM repository site using a valid email address.
It is time now to publish your module −
If everything is fine with your module, then it will be published in the repository and will be accessible to install using NPM like any other Node.js module.
Node.js Related Interview Questions
|HTML 5 Interview Questions||Java Script Interview Questions|
|PHP Interview Questions||Ruby on Rails Interview Questions|
|Angular JS Interview Questions||Ruby Interview Questions|
|Ext JS Interview Questions||Java Abstraction Interview Questions|
|Koa.js Interview Questions||Backbone.js Interview Questions|
|D3.js Interview Questions||Nginx Interview Questions|
|NoSQL Interview Questions||Phonegap Interview Questions|
|MongoDB Interview Questions||Advanced jQuery Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.