How to fix npm: command not found

JavaScript and npm are two powerful tools that work together to help you build websites and applications. JavaScript is a programming language that runs in your web browser, and npm is a package manager that helps you install and manage software packages.

One of the many errors that confuses JavaScript beginners is the “npm: command not found” error, which happens when you try to run npm commands such as npm install.

$ npm install -g n zsh: command not found: npm

In this article, we will show you a few solutions to fix “npm: command not found” in major platforms such as Windows, macOS and Linux. The fix may vary between different operating system and configurations, but the principles remain the same.

“npm: command not found” causes

There are several things that can trigger the ‘npm command not found’ error:

  • npm is not installed: If you’ve downloaded Node.js installer from the official website, npm should be installed already with Node.js. But in order to have a proper setup, follow the advice from npm documentation : We strongly recommend using a Node version manager like nvm to install Node.js and npm. We do not recommend using a Node installer, since the Node installation process installs npm in a directory with local permissions and can cause permissions errors when you run npm packages globally.
  • npm is not present in PATH system variable: Most operating systems look for executables and binaries in a specific set of places. The list of all those places is stored in an environment variable, often named PATH.
  • Node.js is outdated while npm is up-to-date: One software package that depends on another expects its companion to be in a specific version.
  • The current user does not have permission to call npm binary

How to fix “npm command not found” error on Windows

Verify Node.js and npm installation

Before going any further and trying any other solutions, make sure you have Node.js and npm already installed. In order to do so, first, open an elevated PowerShell windows by right-clicking on the Windows icon on the taskbar. and select Windows PowerShell (Admin).

windows powershell admin

Then, sequenrially run “node -v” and “npm -v” to print out Node.js and npm version number. If both commands returned, then Node.js and npm are already installed on your system. Otherwise, you have to download the installer from https://nodejs.org/en/download/ and run it with administrator rights.

Check PATH system variable for Node.js path

If you have Node.js and npm installed, yet the “npm: command not found” persists, the path where npm binary is placed in may not be added to PATH.

Press Start key on your keyboard or click Start button and search for Edit the system environment variables.

edit the system environment variables

In the System Properties window , switch to Advanced tab and select Environment Variables…

system properties

In the Environment Variables window, under System variables, look for Path or PATH system variable, then select Edit….

environment variables

In the list, look for C:\Program Files\nodejs. If it’s not presented in the list, to add the path aforementioned to the PATH system variable

Adding the Node executable folder location to Path variable

Update Node.js

Make sure you’re using the latest version of Node.js to avoid any serious issues. Upgrading is easy – just follow these simple steps.

  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“.

Fix “npm command not found” error in Linux

There are many Linux distributions available, but for the sake of simplicity, this article will focus on solutions for Ubuntu. Ubuntu is one of the most popular Linux distributions and has been around since 2004. It is the perfect choice for those looking for a user-friendly option. You can adapt the instructions below to other Linux distribution as well, including Linux Mint, Fedora, RHEL, etc.

Reinstall npm

If you’ve have already installed nodejs and npm, yet still getting “npm: command not found”, you can try reinstalling it once again by running these commands against a terminal window.

sudo apt-get purge npm node sudo apt-get install npm node -y
Code language: JavaScript (javascript)

Reload or restart VSCode

If you’ve installed npm globally on your system, and you’re getting the error message from VSCode terminal, it might be the terminal that didn’t update the VSCode application. Just close all VSCode windows and reopen the application again to reload all of its module – that should be enough to resolve the issue.

Add npm to PATH system variable

Just like Windows, Linux uses the PATH environment variable to find binaries for each command that run against the terminal. Follow these instructions to add npm to PATH :

  • Open a terminal window pressing CTRL+ALT+T keyboard combination
  • Run which npm to locate the path where npm is placed. You’ll get something like /usr/bin/npm
  • Now run export PATH="/usr/bin:$PATH" to add npm binary location to PATH system variable.
  • You may have to restart the computer for the changes to take effect.

Install Nodejs and npm using nvm

To install or update nvm, you should run its install script. To do that, you may either download and run the script manually, or use the following cURL or Wget command. Please note that the command is ran

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash # OR wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Code language: PHP (php)

Running either of the above commands downloads the nvm installation script and runs it. The script essentially clones the nvm repository to ~/.nvm, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
Code language: PHP (php)

At this point, you may need to log out of your account or reboot the computer for the changes to take effect. Once nvm is installed, you can install the current LTS (Long Term Support) version of Node.js and npm with the following command :

nvm install --lts

The output may look something like this.

Installing latest LTS version. Downloading and installing node v10.16.3... Downloading https://nodejs.org/dist/v10.16.3/node-v10.16.3-darwin-x64.tar.xz... ######################################################################## 100.0% Computing checksum with sha256sum Checksums matched! Now using node v10.16.3 (npm v6.9.0) Creating default alias: default -> lts/* (-> v10.16.3)
Code language: PHP (php)

Give the current user permission to npm path

In rare cases, npm: command not found can be the result of users not having the permission to read and execute the binary. You can easily verify this by prefixing any command with sudo to run it with root privileges. For example, sudo npm --v.

This is often the case with Linux and macOS users. In order to fix any permission

sudo chown -R $(whoami):admin /usr/local/lib/node_modules/
Code language: JavaScript (javascript)

The command recursively give the current user the permissions to /usr/local/lib/node_modules/, which is where your node packages are placed. After running the above command,  try the npm command you were attempting again.

Fix “npm command not found” error in macOS

Homebrew is a free and open-source software package management system for Apple’s macOS and Linux operating systems, allowing users to easily install software.

While not being pre-installed on every macOS device, it’s a third-party package manager that administrators never knew they needed until they tried it out

The lightweight package manager is based on Ruby and uses GitHub to fetch installs, update apps, and add repositories where developers can add applications to be managed through command-line interfaces.

Currently, this is by far the easiest way you can get Node.js and npm installed on your macOS-based computer.

In order to install Homebrew, carefully copy and paste the one-line installation command and paste it into Terminal.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Code language: JavaScript (javascript)

If you’re wondering why the command includes /bin/bash instead of ruby like usual, the answer is Homebrew installer has been rewritten in Bash recently to ensure better compatibility.

The Homebrew installer does automatically add brew to PATH, but sometimes, the process may have been interrupted. In that case, you may want to check out our guide on how to fix “brew: command not found” error.

Conclusion

The “npm command not found” error can be a real pain when you’re working to develop an application and need access to an important directory like npm. Without npm, you don’t have access to the libraries for Node.js, which can significantly increase the time it takes to code common features.

If you likes to code in JavaScript, you may be interested in our post on JSON’s end of file expected, which is a very common error that confuses beginners.

Click to rate this post!
[Total: 13 Average: 4.8]

Leave a Comment