What is npm
npm is a short form of Node Package Manager, which is the world's largest software registry. The open-source web project developers use it from the entire world to share and borrow packages. The npm also acts as a command-line utility for the Node.js project for installing packages in the project, dependency management, and even version management.
Components of npm
npm mainly consists of three different components, these are:
Installing npm (Node.js and npm)
npm comes with Node.js, which means you have to install Node.js to get installed automatically on your personal computer. There are two different methods to install npm on a computer.
npm installation is highly recommended using nvm (Node Version Manager) rather than using Node installer. The Node installation process installs npm in the directory by allowing local permissions, and it generates an error message when you try to run npm packages globally.
1. Install npm and Node.js using nvm
It is highly recommended by npm's to use Node version manager to install npm and Node.js on your device. Installing node.js using nvm allows installing different versions of Node.js on your computer, and you can use any Node.js version for your project by switching as per your requirement. They do not recommend to us Node installers to install npm and Node.js.
To download and install npm using nvm on your Linux operating system, open https://github.com/nvm-sh/nvm.
Check nvm version
After the nvm setup gets installed, ensure whether you get installed or which version on nvm is installed. To do this, run the following command on your terminal:
The above command displays the nvm version running on your device and another nvm command with their short descriptions.
You can also check the nvm version using the command: nvm version.
Check the list of Node.js available to install using nvm
To find the supported Node.js version to install using nvm, use the following command. It will display the partial available list of Node.js versions.
In the above list, CURRENT refers to the current version of Node.js, and LTS refers to the long-term support version.
Installing Node.js and npm
You can choose any version of Node.js to install; we are installing LTS 14.15.4 for our machine. Simply run the following command to install Node.js:
This command installs Node.js (64-bit or 32-bit) and npm together depending on your device.
Check the version of Node.js and npm
To use Node.js use the command nvm use 14.15.4 (make sure to replace 14.15.4 with your installed version).
After that, run the command node -v and npm -v on your terminal to check the version of Node.js and npm, respectively.
Check the list of Node.js versions installed.
You can even check the total number of Node.js versions installed and running on your computer using the below command.
Currently, we have the Node.js version (14.15.4) running on our device. We can also be able to install more different versions of Node.js on a computer if required (while building another Node.js project based on a different version). To install another version of Node.js, choose a version from the nvm list available and run the command given below.
Here, we have selected another Node version 12.20.2 to install, and it also downloads and installs compatible npm on our device.
Now, we have two different versions of Node.js and npm. To check the total number of installed Node.js version, run the following command:
Now, we have two different versions of Node.js and npm on our computers.
2. Install npm and Node.js using Node installer
Suppose you are not able to install, or you are facing some difficulty while installing npm using Node version manager on your device. The other way to install both npm and Node.js on your device is using the Node.js installer.
Navigate to the Node.js download section, where you will find Node.js installer for the different operating systems to install Node.js and npm together.
Depending on your computer operating system and its bits, download any supported Node.js installer.
The additional tools are ready to install, as we marked above to install additional tools automatically. Press any key to continue.
Run the following command on the command line to download the latest version of npm.
Properties of npm:
version: it is the version of your project
description: pass the project's description information
license: it is a project's license
When to use npm
npm includes a CLI (Command Line Client) that can be used to download and install npm packages (software):
On Windows OS Example
On Mac OS Example
Publishing a Package
You can publish any directory from your computer as long as the directory has a package.json file.
Check if npm is installed:
Check if you are logged in:
If not, login:
Navigate to your project and publish your project:
Installing Packages in Global Mode
Look at the output screen above; one additional package is installed in Global Mode. It is a UglifyJS's dependencies.
Installing Packages in Local Mode
When we usually install any packages locally, we normally do so using a package.json file. Let's see an example of creating a project folder in our home directory:
Press Enter to create package.json as defaults, and again confirm the .json if it is ok by a command "yes." This will create a package.json file at the root of the project:
Uninstalling Local Packages
As the npm is a package manager and can be manipulated at any time, we can also remove or uninstall a package. Suppose that the currently installed package packageName is causing a compatibility problem. So we will remove this package and install an older version:
Installing a Specific Version of a Package
Now, we can install the packageName package of the specific version we want. We can do it by using the @ sign to append a version number.
As we have seen, there are several ways to run npm commands. Below are the brief lists of some of the commonly used npm commands (or aliases).