How to fix the NPM command not found error?

In this article, we will go through different solutions that can help you fix the common issue "npm command not found" effortlessly. 

Updated: 11 May, 23 by Susith Nonis 5 Min

List of content you will read in this article:

NPM is a top-rated package manager for Node.js applications. However, in some cases, a user may find trouble installing it or, by any chance, finds it difficult to resolve the “npm package missing” error. This article will go through different solutions that can help you fix this common issue quickly.

NPM, or Node Package Manager, is a NodeJS package manager. A package manager allows you to specify the dependencies for your project, stating that this tool or site requires these specific third-party components in order to run (or develop). It will then figure out what they need and download it for them automatically. The following are major advantages of using the npm package manager:

  • It deals with recursive dependencies.
  • The dependencies can be re-assembled as needed and do not need to be stored in your repository.
  • You can easily specify and change the version of these packages on which you rely.

The "npm command not found" error typically occurs when the Node.js package manager (npm) is not properly installed or its executable is not added to the system's PATH environment variable. Here are a few possible causes for this error:

Node.js not installed

If Node.js is not installed on your system, it will not include npm by default. You need to install Node.js, which includes npm as a package manager.

Incomplete installation

If Node.js is installed but npm is missing, it may be due to an incomplete installation. In some cases, when you download Node.js, it might exclude npm or encounter errors during the installation process.

Incorrect PATH configuration

The PATH environment variable is a list of directories where the operating system looks for executable files. If the directory containing the npm executable is not included in the PATH variable, the system won't be able to locate and execute npm.

Operating system-specific issues

On some operating systems, there can be additional configuration steps required to ensure npm is accessible. For example, on macOS and Linux, you may need to use a package manager like Homebrew or nvm (Node Version Manager) to install Node.js and npm.

Corrupted installation or conflicts

In rare cases, a corrupted installation or conflicts with other software on your system can lead to the "npm command not found" error. This can happen if there are multiple installations of Node.js or if there are conflicts with other package managers.

This is a standard error that people face in any Operating System. There can be many reasons for this error: system or path variable has been modified, installation mistake, permission issue, etc. Let’s look at possible fixes for this error.

Solution 1 on Windows

Go to this website and download Node.js. Select “Windows Installer” with the “.msi” extension and choose your suitable architecture from 64 or 32-bit options. 

Once Node.js has been installed, the installation procedure will start. Accept the terms and click on Next.

The following dialogue will ask you for the location of the installation of Node.js. Choose any location that suits you.

The following window will ask you to install additional tools along with Node.js. Leave it as the default unless very necessary.

The following window will ask you to install Chocolatey along with Node.js. Again, leave it as the default unless very necessary.

The following window will ask you to start the installation. Click on Install. The installation will then begin.

After installation, verify the npm installation using the following command

> npm -v

Solution 2 for Windows

The path variables are by default added in Windows while installing Node.js. However, some other processes can cause it to get removed or modified. 

To fix this, go to Edit the system environment variables from the Start menu.

 

A new dialogue box saying System Properties will appear. Click on the Environment Variables option from it. 

Select the option named Path from the list and click on Edit.

If the path for Node.js is not present, click on New and paste the location of the Node.js folder. In our case, it was “C:\Program Files\nodejs.”

Solution 1 for UNIX-like systems

For Linux users, type the following sequence of commands to install npm and Node.js

$ sudo apt-get update

$ sudo apt-get install nodejs

$ sudo apt-get install npm

Solution 2 on UNIX-like systems

There can be permission issues in UNIX-like systems. 

To give the required permission to the user, type the below commands on respective Operating Systems and press Enter.

Mac:

$ sudo chown -R $(whoami):admin /usr/local/lib/node_modules/

Linux:

$ sudo chown -R $(whoami):root /usr/local/lib/node_modules/

Solution 3 on UNIX-like systems

You might also not be using the latest version of npm. Run the following command to upgrade npm:

$ sudo npm install npm@latest -g

This article went through various solutions for the “npm command not found” error. We saw how we could install it from scratch and configure some settings if it is already installed and fix this issue. We covered aspects like permission issues, installation issues, path variable issues to fix this error.

People also read:

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.