. Advertisement .
. Advertisement .
The “npm command not found” error responded by the command line may catch many users off guard. But it is actually a common issue when launching programs on the command line. Here is why it happens and what you can do to fix it.
Causes Of The “npm command not found” Error
If you enter the npm command to your system’s command line, the operating system needs to know the location of the executable file of the npm program to run it. However, it can’t go through the entire system every time due to performance reasons.
All major platforms (Linux, Windows, and macOS) put these executable files (also known as binaries or simply executables) in certain places for easier searching. They use an environment variable called PATH for this purpose.
This variable comes predefined with your operating system. In short, PATH is a chain of directories. Typically, each user has their own PATH variable. It can be changed if you want to add (or remove) a directory from the list.
The shell will read the values of environment variables during its initialization, including PATH. When the user inputs a command, the shell will look for the executable in those locations.
If you run into the “npm command not found” error, there are two scenarios:
- You haven’t installed npm in your system, or you have a corrupted installation. A fresh installation should easily solve the problem.
- The directory containing the npm isn’t included in the PATH variable. Adding the correct path will allow the shell to find the program.
Fix The “npm command not found” Error In Linux And BSD
The official package managers of popular Linux distributions can install npm right from the command line. This step is quite straightforward and should not take you more than a few seconds.
Depending on your distribution, npm can be either a standalone package or part of the bigger Node.js package.
Users of Debian, Ubuntu, and Linux Mint can run this command:
$ sudo apt install npm
For Fedora, Red Hat, and CentOS:
$ sudo dnf install nodejs
For Arch Linux:
$ sudo pacman -S npm
$ sudo pkg install node
You will need to specify the version of Node.js when installing it on openSUSE. For example:
$ sudo zypper install nodejs14
For other distributions, get your command from this official list.
When installed through the official channels, there is a low chance your Linux or BSD system couldn’t find the npm binary.
Display the PATH variable using the echo command:
$ echo $PATH
It should see something like this:
The npm program is typically placed in /usr/local/bin or /usr/bin. If they are included in PATH, you should add them back by editing your shell configuration file.
Add this line:
export PATH=$PATH:/usr/local/bin:/usr/bin to ~/.profile, ~/.bashrc/, or ~/.bash_profile. Log out and log in to your system again.
Fix The “npm command not found” Error In Windows
Go to Node’s official download page and grab the correct MSI installer, depending on whether your Windows system is 32-bit or 64-bit.
Click on the .msi file and follow the instructions to finish the setup.
In Control Panel, choose System. Click on Advanced system settings on the left panel. Choose the Environment variable in the Advanced tab.
Select PATH and click Edit under the System variables list. Click Add and browse to the “C:\Program Files\nodejs” directory to add it to the PATH variable.
Click Okay. Restart your computer for the changes to take effect.
Fix The “npm command not found” Error In macOS
Get the official .pkg installer at Node’s website.
Click on the installer file and follow the on-screen instructions to finish the setup.
Open Terminal and open the /etc/paths file:
sudo nano /etc/paths
Add the necessary directory (/usr/local/bin) if it is absent. Save the file, log out and log in again.
If you encounter the “npm command not found” error, it means either you don’t have npm installed, or your system doesn’t know where to look for it. Some simple steps can easily fix this and allow you to run the program in the command line as expected.