npm-command-not-found-fix

How to Fix “npm command not found” Error {Node JS} – 6 Solutions

This post is for all javascript newbies. If you are getting an “npm command not found” error and want to resolve it. You have landed on the right page. When I installed this node package manager first time, I also got this error. You will get 100’s of solution and commands on the internet, but I will give you bullseye commands.

I am sure if you will follow this post step by step. You will be able to resolve the following errors. Doesn’t matter which platform or operating system you are using.

You may also find similar errors related to npm as follows-

  • sudo npm command not found
  • mac npm command not found
  • bash npm command not found
  • npm command not found windows
  • npm command not found ubuntu

This issue may occur when you have recently installed or upgraded npm (node package manager) and node js.

Possible Causes –

  1. npm is not correctly installed
  2. Node js is not installed
  3. Path or system variable changed
  4. Permission issues
  5. Old node version installed

Solution 1 – How to fix npm command not found Windows 10

If you don’t know the pre-requisite and correct way of npm installation. Then, you will definitely land into this npm error situation.

Let me show you the correct way of npm installation in windows.

How to install node js on Windows 10

Watch out this video tutorial to know how to install Node JS and NPM correctly in windows 10. If you want to checkout step by step tutorial, keep reading.

Step1 – Download Node Js

Download Node.js software from nodejs.org website. The latest LTS version is node-v14.15.3. Package npm is in-built. You don’t need to install it separately. If you own an old version of windows, select 32-bit version of node.js installer. Otherwise, choose 64-bit in all other cases.

Download-node-js

Step2– Install Node.js and npm

Double click installer node-v14.15.3-x64.msi and follow the installation wizard.

Step2.1Accept the terms in license agreement and click next.

node-js-EULA

Step2.2 – Select the destination folder for node.js installation. Default location of node.js is “C:\Program Files\nodejs\“. I will recommend keeping it default.

node-js-installation-folder

Step2.3 – Select node.js features. I will suggest you leave it default. Make a note, npm (node package manager) will be there as part of this installation. You don’t need to install it separately.

select-npm-to-fix-npm-command-not-found

Step2.4 – Choose an optional tool (chocolatey) to compile native modules, if required. I will suggest you, keep it unchecked.

node-js-dependent-native-modules

Step2.5 – Click install, Sit back and relax. it will take few minutes.

Fix-npm-command-not-found-windows10
validating-node-js-and-npm-install

Step 3 – Check npm and node Js version.

Check node js version

C:\> node -v 

Check npm (node package manager) version

C:\> npm -v
check-node-js-version

Now you will not get any “npm command not found error” in Windows.

Detailed step by step guide on, how to install Node JS and npm using PowerShell (Chocolatey) tool.

Solution 2 – How to fix npm command not found in Ubuntu

So we have already seen, how to resolve npm command not found error in Windows 10 but what about non-windows users. Let me tell you the steps to get rid of these errors if you are non-Windows users.

  1. sudo npm command not found
  2. bash npm command not found
  3. npm command not found ubuntu

I have taken an example of Ubuntu 20.04 OS and first I will show you new installation process.

Watch out this video tutorial for Node js and npm installation in Ubuntu 20.04. In this video, I have shown, more than one way to install it.

  • Using Ubuntu repository
  • Using Node Source repository

If you like reading, instead of watching video. Keep reading further.

x
NPM not found error in Ubuntu video
How to install node JS and npm in Ubuntu 20.04

Step1 – Update Ubuntu repository first

$ sudo apt-get update
apt-get-update

Step2 – Install Node JS First (Recommended)

Run apt-get command to install nodejs package. Checkout suggested package in output, it says npm.

$ sudo apt-get install nodejs
Install-nodejs-using-apt

Step3 – Install npm (node package manager) in Ubuntu 20.04

Sometimes you scratch your head, Why npm command not found error, even after installing node? Because in Windows, npm is installed with node Js. But in the case of Ubuntu, you need to install it separately.

Now let’s install node package manager (npm) which is required by Node Js. If you will not install this, you will end up with “npm” error.

$ sudo apt-get install npm
Fix-npm-command-not-found-ubuntu

Step4 – Check npm and node js version

Check npm version

$ sudo npm -v 
check-npm-version

Check node version

$ node -v
check-node-js-version

Till this point, you must be good. And further all issues related to “Bash or sudo: npm command not found” will go for sure.

If you are interested in the latest version of npm and node js installation. Checkout out this tutorial on node js and npm using NodeSource repository

Solution 3 – How to Fix npm command not found – Mac

In case you are facing issue “npm command not found” in mac operating system. I will recommend you to remove the existing installation and follow these steps to be successful.

Step1 – Remove node_modules folder

 > rm -rf /usr/local/lib/node_modules

Step2 – Uninstall Node JS using the brew command

> brew uninstall node

Step3 – Install node with –without-npm option.

> brew install node --without-npm

###Then####

> echo prefix=~/.npm-packages >> ~/.npmrc

Step4 – Install npm package using the install script

> curl -L https://www.npmjs.com/install.sh | sh

Step5 – Set environment variable in Bash

> export PATH="$HOME/.npm-packages/bin:$PATH"
> export PATH="$HOME/.node/bin:$PATH"

This will resolve all your npm related errors in mac OS. Watch out for the official nodesource installation guide for further reference.

Solution 4 – Path or system variable changed resulting in npm error

Although, Path and variables are set by default when you install node js and npm in Windows and Ubuntu. But in the rare case, it doesn’t happen. Or maybe you messed up with it. Even in that case, you will get npm command not found error.

Let’s see how to check and setup variable for npm in case of Ubuntu 20.04

How to check the npm path variable in Ubuntu 20.04

First, you need to make sure, npm is installed, if not, follow How to install node JS and npm in Ubuntu 20.04 steps in this post.

Step1 – Check the default path for npm command

$ which npm
Check-npm-path

Step2 – Check /etc/environment file for npm path

Now we need to check, do we have /usr/bin/npm or your npm command path in /etc/environment file.

$ cat /etc/environment
set-npm-path-variable-fix-npm-command-not-found

If you will see, /usr/bin is already part of my environment variable. So I will not get this error. In case you don’t have your npm path in this file, add it.

Step3 – Edit /etc/environment file and add npm path

$ vi /etc/environment

Add npm path till bin folder at the end of environment file separated by a colon “:”. Save and exit from file. Make a note, npm path should be within quotes ” “.

For example, /usr/bin in my case.

Reload environment file for changes.

$ source /etc/environment

How to check the npm path variable in Windows 10

First you need to make sure, node js and npm is installed in Windows 10. If it’s already there, you need to follow these steps

Step1 – Search for environment variable

Step1.1 – Type “environment variable” in search box and click open.

open-environment-variable-windows10

Step1.2 – Click on “Environment variables” in Advanced tab of System properties.

Fix-variable-npm-command-not-found

Step1.3 – Select “Path” and then click Edit.

Edit-environment-variable-npm-command-not-found

Step1.4 – Check for npm path C:\Program Files\nodejs. It is the default target directory for Node.JS and npm.

If npm variable path doesn’t exist, Click New > Add npm variable path “C:\Program Files\nodejs” > Click OK.

Please make a note, you need to replace the default “C:\Program Files\nodejs” with your custom target Folder. In case you have selected a different folder during installation.

add-variable-to-solve-npm-command-not-found

Click OK twice to close all open dialogue boxes.

Solution 5 – Permission issues on “node_modules

if you are getting permission related errors, please run this command to give ownership to your user. It’s applicable to mac and Linux users both.

For Mac users –

$ sudo chown -R $(whoami):admin /usr/local/lib/node_modules/

For Linux users –

$ sudo chown -R $(whoami):root /usr/local/lib/node_modules/

Make sure you provide correct path for node_modules.

Solution 6 – Old Node version installed

If you are running an old version of npm already. You can upgrade it to the latest by using the following command to resolve the npm command not found error.

Run this command, if you are running Ubuntu Linux

$ sudo npm install npm@latest -g

Check Node version

$ npm -v

For Windows users

Run below mentioned commands in PowerShell on by one.

c:> Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force 
c:> npm install -g npm-windows-upgrade 
c:> npm-windows-upgrade

Check Node version

c:\> node -v

FAQs:- NPM command not found

Why NPM command is not working?

The reason may be either npm package is not installed, incorrectly installed or path variable is not set for npm. Install npm and node js package as recommended or setup npm path variable to resolve it.

What is NPM Run command?

npm run-script or npm run (alias) is the command to execute arbitrary package scripts. npm run-script <command> is the syntax. Without <command>, it will list the available scripts.

What does the command npm install do?

The npm install command helps to install a package and it’s dependencies. For further help and syntax run : npm install help

Is not recognized as an internal or external command NPM?

This means npm command is not recognized. Make sure you install node js software. NPM will automatically install with node.js in case of Windows. For non-windows users (Linux or Mac), Install node js first and then npm package individually.

What is Test Command in NPM init?

If you are creating a module in node.js using npm init command. The npm test will help you test the run. It is important while integrating with CI/CD tools. If there are problems with tests. Rollback will be done by your CI/CD and actual deployment will not happen.

Conclusion

I hope, till this time, you would have been fixed your issue of the “npm not found error“. Please comment and let me know, which solution fixed your npm issue.

Please share it on social media page, so that it can be helpful for others also.