Table of Contents Hide
What is npm in Node.js?
Basically, npm is a way to reuse the code from the other developers and also to share your code with them. It has a provision to create versions of your code which helps developers to manage different versions of code.
npm Getting Help
npm has a very good documentation about all the commands. It provides the syntax of all the commands and the option available to execute. There are 2 ways you can get help.
npm help access
This will open the browser and provides you the details about the npm access command. The detailed documentation consists of Syntax/Synopsis, Description, and details about the command usage and execution.
npm access --help
Running the above command will provide you the quick help in the command line on how to use the npm command.
Installing Modules using npm
mpm install <Module Name>
For example, if you would like to install the express framework in Node.js you can install by running the below command.
npm install express
Once the installation is finished you can use the express module in you JS file as below.
var express = require('express');
Global vs Local Installation
By default, npm installs all the node modules locally. The local mode refers to the package installation in node_modules directory lying in the folder where node.js is installed. Locally deployed package are accessible using the require method in the JS files. For example, when we installed the express module, it created node_modules directory in the current directory where it installed the express module.
Command to see the Local installed Modules in Node.js
If you want to verify all the modules installed locally in node.js run the below command and it will display all the modules installed using npm locally.
npm ls -l
How to Install Node Modules Globally?
Globally installed packages/dependencies are stored in the system folder. Such dependencies can be used in the CLI(Command Line interface) function of any node.js but it cannot be imported using the require() method in Node application.
Command to install Node Module Globally using npm
npm install express -g
Command to see Globally installed Modules in Node.js using npm
If you want to verify all the modules installed globally in node.js run the below command and it will display all the modules installed using npm globally.
npm ls -g
Command to update modules in Node.js using npm
Update the package.json in the directory to the version you require and run the following command to update the module.
npm update express
Command to uninstall modules in Node.js using npm
npm uninstall express
Once you run this command you could navigate to the directory node_modules or by typing the npm ls command