Home » What is npm

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:

  1. Website: The npm official website is used to find packages for your project, create and set up profiles to manage and access private and public packages.
  2. Command Line Interface (CLI): The CLI runs from your computer’s terminal to interact with npm packages and repositories.
  3. Registry: The registry is a large and public database of JavaScript projects and meta-information. You can use any supported npm registry that you want or even your own. You can even use someone else’s registry as per their terms of use.

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.

  1. Using nvm (Node Version Manager)
  2. Using Node installer

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.

  1. We will download the nvm-setup.zip file for Windows from Git link as we are using a Windows OS device.
  2. After downloading, run the setup and follow the installation instruction as appears on the screen.
    What is npm
  3. Run the setup either by double-clicking on it or by right-clicking and Run as administration.
  4. Allow the installation by clicking on the Yes button.
  5. Read their license agreement carefully and if you agree with it, then accept it and click on Next button.
    What is npm
  6. Select the destination location where you want to install NVM and click on the Next button. We leave it as the default selected location.
    What is npm
  7. Select the folder location in which the setup will create the symlink and click on the Next button.
    What is npm
  8. Now, your setup is ready to install, click on the Install button to start the installation process.
    What is npm
  9. Now, the nvm-setup is installed on your personal computer, and it will take few seconds to wait to complete it.
    What is npm
  10. Your nvm setup installation gets finished; click on the Finish button to exit from setup.
    What is npm

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.

What is npm

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.

What is npm

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:

What is npm

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.

What is npm

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.

What is npm

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.

What is npm

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:

What is npm

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.

What is npm

Depending on your computer operating system and its bits, download any supported Node.js installer.

  1. We have Windows with the 64-bit operating system, and we are downloading a 64-bit Node.js installer for our device.
  2. After downloading, run the setup and follow the installation instruction as shown on the screen.
    What is npm
  3. Click on the Next button to start the installation process.
    What is npm
  4. Read the end-user license agreement, accept it and click on the Next button.
    What is npm
  5. Choose a custom location of your Node.js and click on Next to install. We keep it as its default location.
    What is npm
  6. Select the feature of Node.js to get installed and click on Next. We keep it as its default feature provided.
    What is npm
  7. If you want to install some other necessary tools (C/C++, Chocolaty) with your node.js and npm, checkmark the box and click on the Next button.
    What is npm
  8. Now, click on the Install button to install Node.js, which also installs npm on your device.
    What is npm
  9. Click on Yes to install Node.js on the popup window.
  10. Click on the Finish button to complete the installation.
    What is npm

The additional tools are ready to install, as we marked above to install additional tools automatically. Press any key to continue.

What is npm

Run the following command on the command line to download the latest version of npm.

Properties of npm:

  • All npm packages are defined in files called package.json.
  • You must write the content of package.json in JSON.
  • At least two fields must be present in the definition file: name and version.

package.json

package.json file will be generated when we run the npm init to initialize a JavaScript/Node.js project with the basic metadata information provided by developers:

name: it is the name of your JavaScript library/project

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

  • Use npm to adapt packages of code for your apps or incorporate packages as they are.
  • npm can be used when you want to download standalone tools right away.
  • Run packages without downloading using npx.
  • Use npm when you need to share code with any npm user, anywhere.
  • npm can be used when you want to restrict code to specific developers.
  • Use npm to create organizations to coordinate package maintenance, coding, and developers.
  • Use npm to manage multiple versions of code and code dependencies.
  • Use npm to update applications easily when the underlying code is updated.
  • Use npm to find other developers who are working on similar problems and projects.

Command-Line Client

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

For the time being, we have only one package installed globally, and this is the npm package itself. So let’s change it and install another package UglifyJS (a JavaScript minification tool). Here, we use the –global flag, but it can be abbreviated to -g:

What is npm

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:

What is npm

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).

  • npm i : install local package
  • npm i -g : install global package
  • npm un : uninstall local package
  • npm up: npm update packages
  • npm t: run tests
  • npm ls: list installed modules
  • npm ll or npm la: print additional package information while listing modules

You may also like