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 -
- npm is not correctly installed
- Node js is not installed
- Path or system variable changed
- Permission issues
- Old node version installed
Articles on troubleshooting, you may find useful -
- Yarn command not found error
- Sudo command not found error
- Fix "mkvirtualenv command not found" error
- Nodemon command not found
- Nodemon app crashed - Waiting for file changes before starting
Table of Contents
- 1 Solution 1 - How to fix npm command not found in Windows 10
- 2 Solution 2 - How to fix npm command not found in Ubuntu
- 3 Solution 3 - How to Fix npm command not found - Mac
- 4 Solution 4 - Path or system variable changed resulting in npm error
- 5 Solution 5 - Permission issues on "node_modules"
- 6 Solution 6 - Old Node version installed
- 7 FAQs:- NPM command not found
- 8 Conclusion
Solution 1 - How to fix npm command not found in 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. If you have npm already installed then skip to solutions 3, 4 or 5 based on what OS you have to fix your path or system variable.
Let me show you the correct way of npm installation in windows.
How to install node js on Windows 10
Watch out for this video tutorial to know how to install Node JS and NPM correctly in windows 10. If you want to check out the step by step tutorial, keep reading.
Step1 - Download Node Js
Download Node.js software from the 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 the 32-bit version of the node.js installer. Otherwise, choose 64-bit in all other cases.
Step2- Install Node.js and npm
Double click installer node-v14.15.3-x64.msi and follow the installation wizard.
Step2.1 - Accept the terms in the license agreement and click next.
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.
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.
Step2.4 - Choose an optional tool (chocolatey) to compile native modules, if required. I will suggest you, keep it unchecked.
Step2.5 - Click install, Sit back and relax. it will take a few minutes.
Step 3 - Check npm and node Js version.
Check node js version
C:\> node -v
Check npm (node package manager) version
C:\> npm -v
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 the 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 a non-Windows user.
- sudo npm command not found
- bash npm command not found
- npm command not found ubuntu
I have taken an example of Ubuntu 20.04 OS and first I will show you the new installation process.
Watch out for 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 videos. Keep reading further.
Step1 - Update Ubuntu repository first
$ sudo 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
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
Step4 - Check npm and node js version
Check npm version
$ sudo npm -v
Check node version
$ node -v
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 the 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 rare cases, it doesn't happen. Or maybe you messed up with it. Even in that case, you will get the npm command not found error.
Let's see how to check and set up variables for npm in the 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
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
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 the search box and click open.
Step1.2 - Click on "Environment variables" in the Advanced tab of System Properties.
Step1.3 - Select "Path" and then click Edit.
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.
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.
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 the 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 the below-mentioned commands in PowerShell one 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 the npm package is not installed, incorrectly installed or the path variable is not set for npm. Install npm and node js package as recommended or set up npm path variable to resolve it.
What is the 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 its dependencies. For further help and syntax run : npm install help
Is not recognized as an internal or external command NPM?
This means the npm command is not recognized. Make sure you install node js software. NPM will automatically install with node.js in the 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 the 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.
How do I know if npm is installed?
Run npm -v command on terminal or command prompt, to check whether npm is installed or not. In case you get "npm command not found error". Either npm is not installed or your path variable is not setup correctly.
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 your social media page, so that it can be helpful for others also.