Introduction
npm is the default package manager for Node.js, the popular runtime environment for executing JavaScript code. The npm package manager consists of a command-line client, also called npm, and an extensive online repository, the npm registry.
Developers use the npm client to access and search the npm registry for JavaScript resources, install and manage packages, and handle versioning and updates for dependencies.
Learn how to fix the npm: command not found error in Windows or Linux and ensure your Node.js projects stay on schedule.
Prerequisites
- Windows: Access to the command prompt (CMD) or PowerShell and a user account with administrator privileges.
- Linux: Access to a command line/terminal window and a user account with sudo or root privileges.
What Causes the “npm: command not found” Error?
The npm: command not found
error indicates that the npm command-line interface (CLI) is not recognized or properly installed on your system.
The main causes of the npm: command not found
error include the following:
1. The npm package manager is not installed.
2. The npm PATH and System Variables settings are incorrect.
3. Conflicting Node.js installations on the same system or an outdated Node.js version.
4. The user executing the command does not have the necessary permissions.
Troubleshooting “npm command not found” on Windows
To fix the npm: command not found
error on Windows:
- Ensure that npm and Node.js are installed correctly.
- Check the PATH and system variables in Windows.
- Remove conflicting Node.js installations.
Check if npm is Installed on Windows
To confirm npm is installed, access the Windows Command Prompt or PowerShell, and use the following command to check the npm version:
npm -v
If npm is installed, the terminal displays the version number.
If you receive the npm: command not found
error, you need to install Node.js and npm.
Note: The npm client is part of the default Node.js package.
To install Node.js and npm on Windows:
1. Visit the official Node.js page and download the latest Node.js Windows Installer.
2. Access the download location and double-click the Windows Installer Package.
3. Select Next on the initial Node.js Setup Wizard screen.
4. Accept the License Agreement and click Next.
5. Set the destination folder for the installation and select Next.
6. Ensure that the npm package manager is part of the installation bundle. Click Next to proceed.
7. The tools for compiling native modules are not mandatory and require an additional 3 GB of space. Check the box if you would like to install these tools and select Next to continue.
8. Click Install to start the installation process.
9. Select Finish to exit the Setup Wizard once the installation is complete.
10. Access the Windows Command Prompt (or PowerShell) and check the Node.js version:
node -v
11. Verify the npm version:
npm -v
Check Path Variables on Windows
The system may not recognize the npm
command due to incorrect or missing environment variables. Follow these steps to check and configure environment variables in Windows:
1. Type environment variables in the search box on the Windows taskbar.
2. Open the Edit the system environment variables panel.
3. Expand the Advanced tab and click Environment Variables.
4. Select Path in the System Variables section and click Edit.
Note: If the Path variable does not exist, click New to create a new Path variable, and add the npm installation directory path.
5. Check if the environment variables list contains the path to the npm installation directory. The default path is C:\Program Files\nodejs.
6. Configure a new path or edit the existing npm directory path if you have installed npm in a different directory.
7. Click OK to save the changes and close all windows.
Restart the command prompt for the changes to take effect.
Remove Conflicting Node.js Installations
Multiple installations of Node.js or npm on the same system can lead to conflicts and the npm: command not found
error. To prevent potential issues, remove conflicting installations and keep only the desired Node.js version.
Use nvm (Node Version Manager) if you need to run and manage multiple Node.js versions on a single machine. The nvm tool enables users to update Node.js versions and downgrade Node.js with a few simple commands.
Troubleshooting “npm command not found” on Linux
Complete the following steps to troubleshoot the npm: command not found
error in Linux:
1. Confirm that npm is installed.
2. Check the npm installation directory PATH and system variables.
3. Check user permissions.
Check if npm is Installed on Linux
To confirm if npm is installed, access the terminal window, and check the npm version using the following command:
sudo npm -v
If you receive the npm: command not found error, install Node.js and npm using the instructions for your Linux distribution.
Install npm on Ubuntu/Debian
Note: Installing Node.js and npm from the official Ubuntu/Debian repository may lead to the installation of an outdated Node.js version. To obtain the latest Node.js version, learn how to install the latest Node.js version on Ubuntu or how to install Node.js on Debian.
1. Update the apt package lists using the following command:
sudo apt update
2. Use the apt command-line tool to install npm:
sudo apt install npm -y
Note: If Node.js is not installed, the system proceeds to install both Node.js and npm from the Ubuntu repository.
3. Once the installation is complete, type npm
to view the command Usage page:
npm
The usage page contains essential commands and information, including the npm version you installed.
Install npm on Rocky/Fedora
Note: Installing Node.js and npm from the official repository of your Linux distribution has the fewest steps but may result in installing an older Node.js version. Use the Node Version Manager (nvm) to install the latest Node.js version on CentOS and other RHEL-based distributions.
1. Enter the following command to install npm on RHEL-based Linux distributions like Fedora or Rocky Linux:
sudo dnf install npm -y
Note: If Node.js is not installed, the system proceeds to install npm, Node.js, and Node.js dependencies.
The Complete! message indicates that npm is installed.
Use the which command to view the npm installation directory path:
sudo which npm
Check Path Variables on Linux
Enter the following command to check if the npm installation directory is in the system’s PATH:
echo $PATH
The output must contain the path to the npm installation directory. The default npm installation directories are /usr/bin/npm
or /usr/local/bin/npm
.
If the npm installation directory path is not present, update the PATH to include the directory where npm is installed.
To set the system’s PATH environment variable in Linux:
1. Use a preferred text editor, in this example, nano, to access the .bashrc file:
sudo nano ~/.bashrc
2. Append the following line at the end of the .bashrc file:
export PATH="$PATH:/path/to/npm"
Replace /path/to/npm
with the npm installation directory path on your system.
3. Save the file and close the text editor.
4. Enter the following command to apply the changes:
source ~/.bashrc
5. Restart the terminal to complete the PATH update.
6. Use the echo command to confirm the new PATH variable has been added:
echo $PATH
Check Permissions on Linux
The system may return the npm: command not found
error if the user lacks the appropriate permissions for the npm installation directory.
npm creates the node_modules directory by default to track locally installed packages via the package.json file.
Use the chown command to give your user the necessary permissions for the node_modules directory:
sudo chown -R $(whoami):root /path/to/node_modules
Replace /path/to/node_modules
with the node_modules path on your system.
Conclusion
You have successfully resolved the npm: command not found error and can continue using npm to install and manage Node.js packages.
Find out how the package.json file works and use it to automate the management of Node.js package dependencies.