How to Resolve npm Command Not Found Error in Node.js

npm command not found error in node js

NPM (Node Package Manager) is a command-line utility for managing JavaScript packages and their dependencies. Considered an essential requirement for Node.js development, NPM streamlines the process of sharing and reusing code. Developers use it to facilitate package installation, version management, and project configuration, enhancing the efficiency of JavaScript development workflows.

Developers infrequently encounter the npm: command not found error, often after a fresh installation of the JavasScript runtime and development environment. While this is a rare error, it can stop all development activities on the affected machine. So, in this tutorial, we’ll show you how to resolve this error in Windows and Linux environments. 

Let’s start with a quick look at the main reasons why you might see this error on your Linux or Windows machines.

Table of Contents

  1. The Root Causes of npm: command not found Error
  2. How to Resolve npm command not found Issue in Windows
  3. Check npm Installation On Windows
    1. Check The Path Variables On Windows
    2. Remove Duplicate Node.js Installations
  4. How to Resolve npm: command not found error in Linux
    1. Check npm Installation on Linux
    2. Check the PATH Variables on Linux
    3. Check Permissions on Linux
  5. Conclusion
  6. FAQs

The Root Causes of npm: command not found Error

At its core, the npm: command not found error means that the system doesn’t know what npm is or it’s not set up correctly.

npm init

Here are the common reasons you might see this error:

  • npm is not installed on your system
  • If you installed npm on your system, the system might not know where to find npm. This could be due to incorrect settings.
  • Having multiple Node.js versions or using an outdated version can lead to this error. You need to ensure compatibility between your OS and the npm version.
  • Lack of appropriate permissions is often the “invisible” reason behind this error.

Now that you know why this error occurs, let’s go into the details of resolving this issue on Windows and Linux systems.

How to Resolve npm command not found Issue in Windows

Windows is a popular development environment for JavaScript, and developers can encounter this error. Let’s see how you can resolve this error.

Before going through the steps outlined below, you should make sure you are logged in as an Administrator and have access to the Command Prompt or PowerShell. 

Here are the areas you should check to resolve the error: 

  • Ensure that npm and Node.js are correctly installed.
  • Look at the PATH and system settings on your computer.
  • Remove any additional or duplicate Node.js versions.

Check npm Installation On Windows

Once you ensure that we’re good to go, let’s check out the first solution for Windows users. 

To check if npm is installed on your computer, open the Command Prompt or PowerShell in Windows and type the following command:

npm -v

Here, the –v flag is used to print the version information. If npm is installed, you’ll see a version number in the command line:

npm -v

If you see the npm: command not found error, you must install Node.js and npm on your computer.

Here’s how to install Node.js and npm on your Windows computer:

  • Go to the official Node.js website and download the latest Node.js Windows Installer.

downloads

  • ind where you saved the downloaded file and open it by double-clicking the Windows Installer Package.

Click the Next button on the initial screen of the Node.js Setup Wizard.

node js setup

  • To proceed, accept the License Agreement and click Next to go to the next screen.

license agreement

  • Choose where to install the program and then click Next.

destination folder

  • On the Custom Setup screen, verify that npm is included in the files to be installed on the system, and click Next.

node js custom setup

  • You also get the option to install additional tools. Please note that this step requires a Python interpreter and VS Build Tools. If you want them, tick the box and press Next to keep going.

node js tools for native modules

  • Click Install to start the installation.

ready install node js

  • When the installation process is done, click Finish to close the Setup Wizard.

complete node js installation

  • Open the Windows Command Prompt (or PowerShell) and see what version of Node.js you have installed with this command:

node -v

node -v

> npm -v

npm -v 2

Check The Path Variables On Windows

Even if the installation goes without a hitch, your Windows machine might still not understand the npm command because of incorrect or missing settings. 

Here’s how to check and fix these settings in Windows:

  • Search for environment variables in the search box on the Windows taskbar.

Click Edit the system environment variables.

Edit the system environment variables

  • Go to the Advanced tab and click Environment Variables.

system properties

  • Find Path in the System Variables and click Edit.

environment variables

  • Go through the list to see the path to the npm installation location. It’s usually in a folder called C:\Program Files\nodejs.
  • If you installed npm in a different folder, you can add the full path to the Path variable. Alternatively, you can edit the Path variable to reflect the current location.
  • Click OK to save the changes and then close all the windows.

edit environment variables

To make the changes work, relaunch the Command Prompt.

Remove Duplicate Node.js Installations

Having more than one set of Node.js or npm can cause conflicts and result in the npm: command not found problem. The simple fix is to remove all extra installations and retain the desired version.

If you do need multiple Node.js and npm versions on your computer, we recommend nvm (Node Version Manager) to manage multiple versions. You can use nvm to switch between different Node.js versions.

How to Resolve npm: command not found error in Linux

Linux is the preferred platform for JavaScript development because it closely mirrors the actual environment the project will run on the production servers.

Resolving the npm: command not found in Linux is a simple matter of going through the following list of causes. Note that you’ll need a user account with sudo privileges and access to a terminal. 

  • Make sure you have npm installed on your Linux machine.
  • Verify the npm installation directory PATH and system variables.
  • See if you have the correct permissions to use npm.

Check npm Installation on Linux

To check if npm is on your computer, launch the terminal and run this command to find out the npm version:

# npm -v

npm -v 3

If you see the npm: command not found error, it’s essential to install Node.js and npm by following the recommended guidelines for your Linux distribution. We’ll quickly go through the process for the two major Linux distribution families:

Install npm on Ubuntu/Debian

Use the following command to refresh the list of the server software packages:

# apt update

apt update

Next, use the following command to add npm to your system:

# apt install npm -y

apt install npm -v

After installing, Enter npm to see the available command option:

# npm

npm

As you can see, the output includes critical information, such as the location of the config files and critical commands.

Install npm on Rocky/Fedora

Enter the following command to get npm on RHEL-based distributions such as Fedora or Rocky Linux:

# dnf install npm -y.

dnf install npm -y

The Complete! message means that npm is ready to use. To find out where npm is installed, use the which npm command:

# which npm

which npm

Check the PATH Variables on Linux

After installation, use the echo command check to see if npm is in the list of locations your computer looks for applications.

# echo $PATH

The output should show the location where npm is installed. Typically, it’s in either /usr/bin/npm or /usr/local/bin/npm.

echo $PATH

If you installed nom in a different location, chances are the PATH variable might not have the relevant information. If that’s the case, update the PATH environment variable by following these steps:

  • Open the .bashrc file with your preferred text editor, like Nano or VI.

# nano ~/.bashrc

  • Add this line to the .bashrc file:

export PATH="$PATH:/path/to/npm"

baschrc

Change /path/to/npm to the actual location where npm is installed on your computer.

  • Save the file and exit the text editor.
  • Reload the new configuration details with the following command:

# source ~/.bashrc

  • Relaunch the terminal to ensure the PATH changes are in effect.
  • Recheck with the echo command to check if the new PATH setting has been added successfully.

# echo $PATH

echo $PATH 2

Check Permissions on Linux

On Linux systems, a lack of permissions can result in the npm: command not found error. 

During the development process, npm creates a node_modules directory to manage installed packages for your project based on the package.json file. If your user doesn’t have proper permissions, you will face errors. 

To resolve the permission problem, use the chown command to grant your user the necessary permissions for the node_modules directory.

# chown -R $(whoami):root /path/to/node_modules

Change /path/to/node_modules to the actual location of the node_modules directory on your computer.

Conclusion

Fixing the npm: command not found error in Node.js is essential for a smooth development experience. By following the simple steps mentioned in this tutorial, you can ensure that Node.js and npm work seamlessly on your system. 

RedSwitches offers the best dedicated server pricing and delivers instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What is npm?

npm (Node Package Manager) is the default package manager for the JavaScript programming language. It is extensively utilized for installing, sharing, and managing package dependencies in Node.js projects.

Q. Why am I encountering the npm command not found error?

The npm command not found error typically arises when the npm is not installed on your system.

Q. How can I resolve the npm command not found error?

You can rectify this error by installing npm on your system and incorporating the directory containing npm into your system’s PATH variable. 

 Q. What are the potential causes of the npm command not found error?

This error may be caused due to npm‘s absence on your system, an incorrectly configured PATH variable, or misconfigurations within your system environment.

Q. How do I verify if npm is present on my system?

Check for npm‘s presence on your system by executing the npm -v command in your terminal. If npm is installed, it will display the version currently in use.

Q. What should I do if npm is not installed on my system?

If npm is not installed, you should install Node.js, which includes npm as its default package manager. Once Node.js is installed, npm becomes readily available on your system.

Q. How can I troubleshoot the npm command not found error in Linux?

To troubleshoot the npm: command not found error in Linux, ensure the directory containing npm is added to your PATH variable. Achieve this by modifying the .bashrc or .bash_profile file in your home directory.

Q. What does the term npm executable refer to?

npm executable denotes the command-line interface for npm, enabling users to interact with the npm package manager by executing simple commands for installing, managing, and publishing packages.

Q. What’s the easy way to rectify the npm command not found error?

To address the npm command not found error, execute the following command in your terminal: export PATH=$PATH:/path/to/npm/directory. Replace /path/to/npm/directory with the actual installation directory of npm on your system.

Q. What steps can I take to ensure proper npm configuration on my system?

Ensure proper npm configuration by confirming that your system’s PATH variable includes the npm installation directory. Additionally, verify the installations of npm and Node.js by running the commands npm -v and node -v in your terminal.

Q. Is it necessary to uninstall the current version of Node.js before installing the latest version?

No, it is not necessary to uninstall the current version of Node.js before installing the latest version. Installing the latest version will seamlessly override the existing installation.

Q. Can you recommend additional resources for troubleshooting npm issues?

For troubleshooting npm-related issues, consider exploring the dynamic Node.js online community. It is an excellent resource for solving problems related to apt-get install, npm files, native modules, user permissions, installation mistakes, error messages, environment files, runtime environments, package locations, and official repositories.