How to Fix “npm command not found” error

Each programming language has its own supporting software, which helps to install important tools. When it comes to JavaScript, npm is the tool that is utilized for downloading additional libraries and packages. The “npm” key term can be used to retrieve and install any packages that are needed. When using this keyword a very common error may occur which is the “npm command not found” problem.

This article will elaborate on what are the root reasons behind this issue as well as how you can fix them.

Resolve the “npm command not found” problem

There are a few different reasons which can be causing this error to pop up on your system. Here, all the possible reasons are discussed below.

Reason 1: Missing npm Tool

As with every “command not found error” the most common reason is that tool is not installed on the system. When this happens, running any “npm install” command will result in the error as shown below:

Solution: Install npm Tool

This issue can be resolved very easily as you simply need to install the npm tool onto your system. To do this, run one of the below-mentioned commands based on your OS.

For Ubuntu:

$ sudo apt install npm

This should successfully install npm onto your system and the error should not occur again.

For CentOS, RedHat, or Fedora:

$ sudo dnf install npm

For Arch Linux and Manjaro:

$ sudo pacman -S npm

Reason 2: PATH Variable is not Set

Often times due to errors, the path, and variables for npm are not set automatically during installation. Due to this, when the npm keyword is called, the system is unable to locate the software correctly and the error is prompted. You can check the path using the following command:

$ which npm

Solution: Set Path of npm

Since the npm file shown in the example above is in usr/bin path we need to add this path into the environment file. Open the environment file in the terminal using the following command:

$ cat /etc/environment

This will show whether the npm path is present or not in the file. If it is not present then add the path that is shown in the snippet above (excluding npm- “/usr/bin/”) to the environment file. For instance, in this scenario, it is just usr/bin. Make sure that the path is within “quotations and separated by a colon. The path is shown below:

If this path is already present, no need to insert it again. Otherwise, insert it and then run the following command to refresh the environment file:

$ source /etc/environment 

Once the Path is set, the error should no longer occur when any npm commands are run.

Conclusion

The “npm command not found” issue arises when the npm tool is missing from the system or its environment path is not set correctly. It can be fixed by either installing the npm tool using an apt installer or setting the path in the environment file. This article has provided a detailed guide on the reasoning behind the problem “npm command not found” as well as methods to fix it.