How to Fix "npm command not found" Error {Node JS} - 8 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 hundreds of solutions 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
  • npm install not working

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. The old node version installed
  6. No package.json file describing the dependencies
  7. Integrity check failed error

Articles on troubleshooting, you may find useful -

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.

Download-node-js

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.

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 as default. Make a note, that 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 a 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 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.

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

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

Step1 - Update the Ubuntu repository first

$ sudo apt-get update
apt-get-update

Step2 - Install Node JS First (Recommended)

Run apt-get command to install nodejs package. Check out the 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 the 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 the NodeSource repository

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

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

Step1 - Remove the 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 the 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 the How to install node JS and npm in Ubuntu 20.04 steps in this post.

Step1 - Check the default path for the 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

As 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 the 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 are 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.

open-environment-variable-windows10

Step1.2 - Click on "Environment variables" in the 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, that 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.

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

Solution 7 - No package.Json file issue

If you run the npm install command and then make sure you have package.json file existing in the same directory. Else you will encounter npm install errors.

  • Run ls -l command to check whether the package.json file exists or not.
$ ls -1
index.js
package-lock.json
package.json
  • Then run npm install command and recommendation are to run it always from the root folder. The npm install command by default install all the packages under the dependencies object of your package.json file.

For example -

{
"name": "sample-app",
"version": "2.0.1",
"description": "A Node app",
"dependencies": {
"jest": "^28.1.1",
"typescript": "^4.5.5",
}
}

In this example, the npm install the command will install jest and typescript packages to the generated node_modules folder as it's mentioned in the package.json file.

Solution 8 - Integrity check failed

If you have a package.json file under your project directory, and you are trying to run the npm install command. The npm will compare the integrity of the package you downloaded with the one specified in the package-lock.json file. In cases it differs, it will throw an error integrity failed check error.

npm ERR! Verification failed while extracting @my-package@^1.3.0:
npm ERR! Verification failed while extracting @my-package@^1.3.0:
npm ERR! Integrity check failed:

To solve this error, you need to move or delete your lock file and clean the npm cache. Follow these steps from your project's root directory.

  • Remove or move your package-lock.json file.
$ sudo rm package-lock.json
or
$ sudo mv package-lock.json /tmp
  • Move or delete the node-modules folder. You can also delete it but I will recommend it to move them to a different or /tmp directory to roll back changes, in case required.
$ sudo mv node_modules /tmp

# in case you want to delete

$ sudo rm -rf node_modules
  • Forcefully clear the npm cache
$ npm cache clean --force

Then run the npm install command again. It should fix your integrity issue.

FAQs:- NPM command not found

  1. 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 the npm path variable to resolve it.

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

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

  4. 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 the npm package individually.

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

  6. How do I know if npm is installed?

    Run the npm -v command on the 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 set up correctly.

Conclusion

Based on the error, follow the required solution from the post. I hope, by this time, you would have 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.