How to fix the ‘npm command not found’ error on Windows, Linux, and Mac operating systems

If you are trying to work on a project in Node.js, and encountering the error ‘npm command not found’ you may have improperly installed the npm repository. In this article, you will find information about JavaScript, Node.js, and npm, as well as the solution to the ‘npm command not found error’. Step-by-step solutions are provided below for the three major operating systems, Windows, Linux, and Mac.

npm command not found error message

JavaScript, Node.js, and npm

npm is the package manager that is associated with JavaScript, and specifically the interpreter Node.js. npm is a repository allowing developers to publish their Node.js projects online, where any other developer or community member can access them. The npm repository hosts both open-source and paid packages.

npm logo

JavaScript is a very popular programming language. JavaScript has been a primary language used to make web applications interactive since HTML5 became standard around 2010. Over the last 10 years, many JavaScript front-end frameworks have been developed, such as Vue, Angular, React, and React Native. A few back-end frameworks have been developed for JavaScript, such as Next, Meteor, and Express. Frameworks allow developers to access a suite of code libraries, to improve development timelines. Instead of writing common code from scratch each time, libraries provide that code already, allowing developers to instead focus their attention on coding custom content and actions.

Node.js is not a back-end or front-end framework, instead Node.js is a runtime environment. Yet, Node.js is the most popular for back-end development of applications. The runtime environment can handle hundreds of calls in the loop simultaneously without waiting for an API to respond. Node.js was developed in 2009 using Google’s V8 JavaScript engine (from Chrome). Since then, Node.js has been adopted by many large companies. Amazon Web Services (AWS) which provides about 33% of all web services, uses Node.js. Walmart, Yahoo!, Microsoft, PayPal, and Netflix are just a few of the other major companies using this runtime environment. 

node.js logo

What causes the ‘npm command not found’ error?

This article has been organized into solutions for different operating systems, to provide an easier way for you to find the solutions that are pertinent to your system. The causes are primarily the same across Windows, Linux, and Mac, but the methods are different.

There are four main causes that can trigger the ‘npm command not found’ error:

  1. npm package manager is not installed
    • When you install Node.js on Windows, npm should automatically be included. However if you selected custom install and didn’t select to install the npm package manager, you may not actually have npm on your system.
    • Unlike Windows, npm is not automatically installed on Linux distributions. If you didn’t select to install npm, the error will continue to appear until you install it.
  2. PATH and system variables are not set correctly
    • When you install Node.js on Windows, the PATH and system variables are automatically set, but if you selected custom install and didn’t select ‘Add to PATH’ you may have an incorrectly set PATH.
  3. Outdated version of Node.js
    • If Node.js is outdated, this can cause the npm package manager commands to fail.
  4. A permission issue is preventing the use of npm commands
    • For Mac and Linux operating systems, you may need to adjust permissions for your npm directory.

How to solve the ‘npm command not found’ error

Windows Operating System

The following solutions are for users and developers trying to solve the ‘npm command not found’ error on a Windows operating system. As Windows 10 is the most updated and supported version of Windows, the step-by-step instructions use Windows 10.

1. Verify/Install Node.js and npm

Before going any further and trying any other solutions, it’s important to verify that Node.js and npm are installed on your system:

  1. Right-click on the Windows icon on the taskbar. Select Windows PowerShell (Admin).
windows powershell admin
  1. Enter “node -v” and Windows Powershell will print the Node.js version number.
  2. Enter “npm -v” to print the npm version number.
  3. If both of the above commands report version numbers, then Node.js and npm are on your system.
  4. Otherwise, you will need to download the installer for Windows (x64). Run the installer and follow the prompts to install the default settings. Npm package manager should be installed with Node.js, as well as online documentation shortcuts, and it will be added to PATH.

2. Check PATH and system variables

To check and fix PATH and system variables for Node.js on Windows:

  1. Click on the Windows icon on the taskbar and search environment variable. Click open for the option listed as Edit the system environment variables.
edit the system environment variables
  1. The System Properties window will open. Select the Advanced tab and then Environment Variables…
system properties
  1. In the Environment Variables window, under System variables, select Path, then Edit…
environment variables
  1. Search the list for C:\Program Files\nodejs. If this path doesn’t exist, you need to create a new one.
edit environment variable
  1. Click New…, then input a Variable name. For the Variable value enter “C:\Program Files\nodejs“. Now click OK

3. Update Node.js

Outdated versions of Node.js can cause serious issues. To upgrade Node to the latest version:

  1. Right-click on the Windows icon on the taskbar. Select Windows PowerShell (Admin).
  2. Run the following commands to update Node.js and npm:
    • Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
    • npm install -g npm-windows-upgrade
    • npm-windows-upgrade
  3. After any packages install and update, check your version of Node.js to ensure it’s updated by entering “node -v“.

Linux Operating System

There are many Linux distributions available, but for the purpose of simplicity, this article will focus on solutions for Ubuntu. Ubuntu boasts the largest Linux distribution market share and has been around since 2004. Other popular distributions include Manjaro, Linux Mint, and Red Hat, but much of the solutions will be applicable to these distributions as well. The solutions are specifically for Ubuntu 20.04.

Below are four solutions to solve the ‘npm command not found’ error on Ubuntu:

1. Install Node.js and npm

Installing Node.js and npm on Ubuntu is fairly simple:

  1. Open a terminal window pressing CTRL+ALT+T simultaneously. Alternatively you can navigate to the Activities menu and search terminal, then clicking on the resulting icon for terminal.
  2. Type the following commands:
    • sudo apt update
    • sudo apt install nodejs npm
  3. When the packages are finished installing, you can verify that both Node.js and npm are installed by entering:
    • node –version
    • npm –version

In this solution, the sudo command gives the user elevated permissions (the same as an administrator). Now that Node.js and npm have been installed and verified, you can attempt to run an npm command.

2. Check PATH and system variables

You now know that npm is correctly installed on your system. Next we will verify the default path for the npm command:

  1. Open a terminal window pressing CTRL+ALT+T simultaneously. Alternatively you can navigate to the Activities menu and search terminal, then click on the resulting icon for terminal.
  2. To check the current path for npm commands enter “which npm“.
  3. The /etc/environment file needs to be checked for the file path from above, which may look something like /usr/bin/npm. Enter “cat /etc/environment“.
  4. This will give you a readout of your environment variable. You want to find the same file path from step 2, in your environmental variable. If not, continue on to add the npm path.
  5. To edit the /etc/environment file enter “vi /etc/environment“.
  6. Now you need to add a colon(:) and the npm path from step 2 to the end of the environmental file. This path file should end at ‘bin’. Make sure this path is before the end of the quotes.
  7. Enter “source /etc/environment” to reload the file with the changes.

3. Give User Permissions to Run Commands

Linux has been designed to be a secure operating system, because most applications have very limited permissions. Sometimes this can cause interference with how programs work. To give yourself permissions to edit node modules (where npm is located):

  1. Open a terminal window pressing CTRL+ALT+T simultaneously. Alternatively you can navigate to the Activities menu and search terminal, then click on the resulting icon for terminal.
  2. Enter “sudo chown -R $(whoami):admin /usr/local/lib/node_modules/“.
  3. Attempt to run an npm command and see if the problem reoccurs.

4. Update Node.js

Finally, older outdated versions of Node.js can cause version issues. To upgrade Node to the latest version, you will use npm:

  1. Open a terminal window pressing CTRL+ALT+T simultaneously. Alternatively you can navigate to the Activities menu and search terminal, then click on the resulting icon for terminal.
  2. Enter “sudo npm cache clean -f” to clear your current cache so that there are no problems accessing the correct file path, if you changed it in the second solution.
  3. Now enter “sudo npm install npm@latest -g“.
  4. You can verify that Node updated to the latest version by entering “npm -v“.

MacOS

1. Install Node.js and npm

Homebrew is a highly popular package manager for MacOS. If you have Homebrew installed on your Mac computer, it is very easy to install Node.js. You should strongly consider downloading Homebrew if you don’t have it. This solution will show you how to install Homebrew, as well as Node.js and npm:

  1. Click the Launchpad icon in the Dock, then search for Terminal. Click the result for Terminal.
  2. Type in the following command into your Terminal window to install Homebrew:
    • /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
  3. Then enter:
    • brew update
    • brew install node
  4. To verify that both are installed, enter “node -v” and “npm -v” to confirm the version of both Node.js and npm.
  5. Check if the ‘npm command not found’ error remains.

Alternatively you can find and use the Node.js installer for macOS.

2. Check PATH and system variables

For macOS, npm is usually found under /usr/local/bin. To check your PATH variable:

  1. Click the Launchpad icon in the Dock, then search for Terminal. Click the result for Terminal.
  2. Into the terminal enter echo $PATH, to display your PATH variable.
  3. Now enter “npm get prefix“. Compare the two outputs. Most likely the npm get prefix command will report /usr/local/bin. This should also be found in your PATH variable.
  4. If the prefix is NOT found in the PATH variable, it needs to be set. To set your PATH enter “export PATH=”[prefix]/bin:$PATH” >> ~/.bash_profile“. [prefix] is the value returned in step 3. Now enter “source ~/.bash_profile“.
  5. Check to see if the ‘npm command not found’ error is now resolved by trying the same command that failed previously.

3. Give User Permissions to Run Commands

MacOS shares a lot of similarities with Linux. Like Linux, you may need to give yourself permissions to edit node modules to bypass this error:

  1. Click the Launchpad icon in the Dock, then search for Terminal. Click the result for Terminal.
  2. Enter “sudo chown -R $(whoami):admin /usr/local/lib/node_modules/“.
  3. Attempt to run an npm command and see if the problem reoccurs.

4. Update Node.js and npm

If you installed Homebrew from the first MacOS solution, this will be extremely simple. If you don’t have Homebrew or another package manager like Nvm or N, you need to visit the Node.js website to find the newest installer for the updated version. Make sure to follow the default installation settings and don’t choose custom install unless you know what you are doing.

  1. If you have Homebrew, click the Launchpad icon in the Dock, then search for Terminal. Click the result for Terminal.
  2. Enter “brew upgrade node“.
  3. That’s it! Double-check by entering “node -v” and “npm -v” to ensure you are up-to-date.

Key Takeaway

The ‘npm command not found’ can be a major pain when you are working to develop an application and can’t access an important directory like npm. Without npm you don’t have access to the libraries for Node.js, and will spend significant extra time coding simple and common features by hand.

This article was broken down into three major sections for the operating systems Windows, Linux, and MacOS. This was to allow you easy access to solutions that would be helpful for your system, without requiring you to wade through non-useful information.

The four primary causes of the ‘npm command not found’ error are Node.js and/or npm not installed, path and system variables not set properly, outdated Node.js, and permissions conflicts. The solutions across all three operating systems are fairly simple and straightforward. If you were not familiar with command line (terminal, bash, etc) commands, you may have learned some useful commands in these solutions. Let us know in the comments section which solution worked, or if we have left out anything!

Leave a Comment