How to Fix “npm command not found” Error {Node.js} – 8 Solutions

If you’re a Node.js developer, npm is an essential package manager that helps you install and manage modules for your applications. However, encountering the “npm command not found” error can be frustrating and prevent you from using npm. This error occurs when your computer cannot locate the npm executable file, which could result from installation problems, system path issues or permission limitations.

Before you attempt to resolve this issue, ensure that both Node.js and npm are installed on your computer by typing “node -v” and “npm -v” in a terminal or command prompt window. If these commands don’t work, download and install the latest versions of Node.js and npm for your operating system.

If Node.js and npm are already installed, but you’re still getting the error message, it’s possible that your computer can’t find the executable file because of system path issues. You can solve this by adding the directory containing the npm executable file to your system path by editing the PATH environment variable. The steps to take this will differ depending on your operating system.

Alternatively, if there is a corrupted installation of npm on your computer due to installation problems or update issues, strange behaviour with using npm commands or error messages may occur. In such cases, fixing a corrupted installation should resolve the problem by executing “npm” command without difficulty.

Introduction

NPM is a package manager for node.js. It helps you to install packages or modules for Node.js applications.

You get an npm error, when your operating system can’t find the npm executable. If you are getting any of the following errors related to npm, follow the article –

  • 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 and Node.js are not installed
  2. System PATH is not setup correctly
  3. Permission issues
  4. No package.json file describing the dependencies
  5. Integrity check failed error

Tutorials, you may find useful –

Solution 1 – How to fix the npm command not found in Windows 10/11

If you don’t know the prerequisite 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.

Check out my script to automatically fix your npm errors for Linux and Mac users. Else you can look for these manual methods.

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

How to install Node.js on Windows 10/11

Step1 – Download Node.js

Download Node.js software from the nodejs.org website. The latest LTS version is node-v18.16.0 as per the nodejs website. The package npm 9.5.1 is included with node.js, so you don’t need to install it separately.

Download latest node.js for Windows

Step2– Install Node.js and npm

Double-click installer node-v18.16.0-x64.msi and follow the installation wizard.

Step 2.1Accept the terms in the license agreement and click next.

node-js-EULA

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

node-js-installation-folder

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

Step 2.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 the npm and Node.js versions.

Open the command prompt and run the following commands to confirm node.js and npm installation.

Check the node js version

C:\> node -v 

Check the 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 the npm command not found error in Linux

I have taken an example of Ubuntu 22.04 OS for output shared in the tutorial.

Debian-based Linux Distributions

  1. Open a terminal window on your Debian-based Linux distribution.
  2. Update the package list by running the following command: sudo apt update
  3. Install the Node.js package by running the following command: sudo apt install nodejs. It will install the npm package also.
  4. Verify that Node.js and npm are installed by running the following commands:
    • node -v (This should print the version number of Node.js.)
    • npm -v (This should print the version number of npm.)

Below-mentioned is the screenshot for your reference. I have installed the latest version of node v19.9.0 and npm 9.6.3 for Ubuntu 22.04

Fix npm command not found error in Linux

Red Hat based Linux Distributions

  1. Open a terminal window on your Red Hat based Linux distribution.
  2. Update the package list by running the following command: sudo dnf update
  3. Install the Node.js package by running the following command: sudo dnf install nodejs. The NPM package will automatically install
  4. Verify that Node.js and npm are installed by running the following commands:
    • node -v (This should print the version number of Node.js.)
    • npm -v (This should print the version number of npm.)

Congrats! you have fixed the “Bash or sudo: npm command not found” error by installing npm and Node.js.

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 Node Source repository

Solution 3 – How to Fix npm command not found error on macOS

Run node -v and npm -v commands to check the existing installation, if you are still facing npm-related errors, you can remove the existing installation and then install it again.

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 errors in macOS. Watch out for the official node source 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 PATH variables for npm in the case of Ubuntu 22.04

How to check the npm path variable in Ubuntu 22.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 the 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 the npm path till bin folder at the end of the environment file, separated by a colon “:”. Save and exit from the file. Make a note, the 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

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

Step1 – Search for the environment variable

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

open-environment-variable-windows10

Step 1.2 – Click on “Environment variables” in the Advanced tab of System Properties.

Fix-variable-npm-command-not-found

Step 1.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 the 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/

or

$ sudo chown -R $(whoami) ~/.npm

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

Frequently asked questions

  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 the npm and Node.js packages as recommended, or set up the npm path variable to resolve it.

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

  3. NPM is not recognized as an internal or external command.

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

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

  5. 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 the “npm command not found error”. Either npm is not installed or your path variable is not set up correctly.

  6. Why is npm not working even after installing Node.js?

    This could be due to a problem with your system path. Make sure that the directory containing the npm executable file is added to your system path.

  7. What should I do if I still get the “npm command not found” error after following these steps?

    If you’ve tried all the steps above and are still having issues, then it might be worth posting on a forum or reaching out to the Node.js community for help.

  8. Can I use a different package manager besides npm?

    If you are still having issues with your npm installation, you can try using a package manager like yarn. Yarn is an alternative package manager that is compatible with npm and can be used as a drop-in replacement. You can install yarn using the following command:
    npm install -g yarn
    Once the yarn is installed, you can use it to install packages in place of npm.

Conclusion

In this tutorial, we discussed the causes of the “npm command not found” error and how to fix it. If you encounter this error, the first thing you should check is the installation of Node.js and npm. If the installation is successful, check the path and permission issues. By following the steps mentioned in this article, you should be able to fix the “npm command not found” error and continue your work with Node.js and npm.

Dev

I'm Dev, your friendly neighbourhood Tech Savy. I spend my days with gadgets, servers, and the occasional laugh-inducing software mishap. Think of me as your tech-savvy, glitch-prone buddy. If you've got questions, feedback, or just need someone to blame when your Wi-Fi goes haywire, I'm your guy!