When we say about the NPM(Node Package Manager) this will provides two main functionalities:-
- This will provides online repositories for node.js packages/modules which are searchable on search.nodejs.org
- This will provides command line utility to install Node.js packages, do version management and dependency management of Node.js packages.
And NPM will comes bundled with Node.js installables in versions after that v0.6.3. And we can check the version by opening Node.js command prompt and typing the following command:
npm version
Installing Modules using NPM
Below is the syntax to install any Node.js module:-
npm install
Now let's install a famous Node.js web framework called express:
Open the Node.js command prompt and execute the below command:
npm install express
Once you install you will see the result after installing the "express" framework.
Global vs Local Installation
When we say dependency in NPM, by default npm installs depedency in local mode. And here mode specifies the folder where Node application present. To understand this we will take an example:- if you installed express module, it created node_modules directory in the current directory where it installed express module.
Well we can use npm ls command to get the list of all locally installed modules.
Now you need to open Node.js command prompt and execute "npm ls"
Globally installed packages/dependencies are stored in system directory. Now let's install express module using global installation. Although it will also produce the same result but modules will be installed globally.
Open Node.js command prompt and execute the following code:-
npm install express -g
Here first line will tells about the module version and its location where it is getting installed.
Unintalling a Module
And to uninstall a Node.js module use the below command:
npm uninstall express
The node.js module is uninstalled. YOu can verify by using the below command:-
npm ls
You can see that the module is empty now.
Searching a Module
"npm search express" command is used to search express or module.
npm search express