How to Fix the "npm: command not found" Error

June 22, 2023

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.

Instructions on how to fix the npm command not found error in Windows and Linux.

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 npm command not found error in a terminal window.

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.

The npm version in the Windows cmd.

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.

Downloading the Windows Installer for Node.js.

2. Access the download location and double-click the Windows Installer Package.

3. Select Next on the initial Node.js Setup Wizard screen.

The Node.js setup wizard in Windows.

4. Accept the License Agreement and click Next.

Accept Node.js license agreement.

5. Set the destination folder for the installation and select Next.

Setting up the Node.js installation folder in Windows.

6. Ensure that the npm package manager is part of the installation bundle. Click Next to proceed.

Ensure npm package is part of the Node.js installation bundle.

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.

Install additional Node.js tools.

8. Click Install to start the installation process.

Installing Node.js on Windows.

9. Select Finish to exit the Setup Wizard once the installation is complete.

Finish Node.js installation in Windows.

10. Access the Windows Command Prompt (or PowerShell) and check the Node.js version:

node -v

Checking the Node.js version in the Windows cmd.

11. Verify the npm version:

npm -v

The npm version in the Windows cmd.

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.

Accessing the Path variables in Windows.

3. Expand the Advanced tab and click Environment Variables.

Access environment variables in Windows.

4. Select Path in the System Variables section and click Edit.

Editing system variables in Windows.

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.

Edit npm environment variable in 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
Checking the npm version in Ubuntu.

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
Updating apt package lists in Ubuntu.

2. Use the apt command-line tool to install npm:

sudo apt install npm -y
Command to install Node.js and npm in Ubuntu.

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 npm usage page in Ubuntu.

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.

Installing Node.js and npm on Fedora.

The Complete! message indicates that npm is installed.

Use the which command to view the npm installation directory path:

sudo which npm

The npm installation directory path in Fedora.

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.

The PATH variables in Ubuntu.

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"
Path to npm in the .bashrc file.

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
Adding npm path to PATH system variable.

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.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
Yarn vs NPM: A Comprehensive Comparison
November 4, 2021

Yarn and NPM are among the most popular package managers for Node.js. This article compares and contrasts their features to help you determine which you should choose.
Read more
How to Build a Node.js App with Docker
April 21, 2023

This guide shows a step-by-step example to build a Node.js application with Docker.
Read more
bashrc vs. bash_profile: What Is the Difference?
September 22, 2022

The Bash shell holds custom information in hidden files such as .bashrc and .bash_profile. But what's the difference between the two? Find out when to use which file in this guide.
Read more
How to Install & Setup MEAN Stack on Ubuntu (MongoDB, Express.JS, Angular.JS, Node.JS)
March 28, 2024

The MEAN stack is an open-source JavaScript (JS) framework used for developing robust web applications. It is a set of software tools that include everything needed for building dynamic websites and web applications.
Read more