Node js npm not found windows

If you are trying to work on a project in Node.js, and encountering the error ‘npm command not found’ you may have improperly installed the npm repository. In this article, you will find information about JavaScript, Node.js, and npm, as well as the solution to the ‘npm command not found error’. Step-by-step solutions are provided below for the three major operating systems, Windows, Linux, and Mac.

npm command not found error message

JavaScript, Node.js, and npm

npm is the package manager that is associated with JavaScript, and specifically the interpreter Node.js. npm is a repository allowing developers to publish their Node.js projects online, where any other developer or community member can access them. The npm repository hosts both open-source and paid packages.

npm logo

JavaScript is a very popular programming language. JavaScript has been a primary language used to make web applications interactive since HTML5 became standard around 2010. Over the last 10 years, many JavaScript front-end frameworks have been developed, such as Vue, Angular, React, and React Native. A few back-end frameworks have been developed for JavaScript, such as Next, Meteor, and Express. Frameworks allow developers to access a suite of code libraries, to improve development timelines. Instead of writing common code from scratch each time, libraries provide that code already, allowing developers to instead focus their attention on coding custom content and actions.

Node.js is not a back-end or front-end framework, instead Node.js is a runtime environment. Yet, Node.js is the most popular for back-end development of applications. The runtime environment can handle hundreds of calls in the loop simultaneously without waiting for an API to respond. Node.js was developed in 2009 using Google’s V8 JavaScript engine (from Chrome). Since then, Node.js has been adopted by many large companies. Amazon Web Services (AWS) which provides about 33% of all web services, uses Node.js. Walmart, Yahoo!, Microsoft, PayPal, and Netflix are just a few of the other major companies using this runtime environment. 

node.js logo

What causes the ‘npm command not found’ error?

This article has been organized into solutions for different operating systems, to provide an easier way for you to find the solutions that are pertinent to your system. The causes are primarily the same across Windows, Linux, and Mac, but the methods are different.

There are four main causes that can trigger the ‘npm command not found’ error:

  1. npm package manager is not installed
    • When you install Node.js on Windows, npm should automatically be included. However if you selected custom install and didn’t select to install the npm package manager, you may not actually have npm on your system.
    • Unlike Windows, npm is not automatically installed on Linux distributions. If you didn’t select to install npm, the error will continue to appear until you install it.
  2. PATH and system variables are not set correctly
    • When you install Node.js on Windows, the PATH and system variables are automatically set, but if you selected custom install and didn’t select ‘Add to PATH’ you may have an incorrectly set PATH.
  3. Outdated version of Node.js
    • If Node.js is outdated, this can cause the npm package manager commands to fail.
  4. A permission issue is preventing the use of npm commands
    • For Mac and Linux operating systems, you may need to adjust permissions for your npm directory.

Windows Operating System

The following solutions are for users and developers trying to solve the ‘npm command not found’ error on a Windows operating system. As Windows 10 is the most updated and supported version of Windows, the step-by-step instructions use Windows 10.

1. Verify/Install Node.js and npm

Before going any further and trying any other solutions, it’s important to verify that Node.js and npm are installed on your system:

  1. Right-click on the Windows icon on the taskbar. Select Windows PowerShell (Admin).
windows powershell admin
  1. Enter “node -v” and Windows Powershell will print the Node.js version number.
  2. Enter “npm -v” to print the npm version number.
  3. If both of the above commands report version numbers, then Node.js and npm are on your system.
  4. Otherwise, you will need to download the installer for Windows (x64). Run the installer and follow the prompts to install the default settings. Npm package manager should be installed with Node.js, as well as online documentation shortcuts, and it will be added to PATH.

2. Check PATH and system variables

To check and fix PATH and system variables for Node.js on Windows:

  1. Click on the Windows icon on the taskbar and search environment variable. Click open for the option listed as Edit the system environment variables.
edit the system environment variables
  1. The System Properties window will open. Select the Advanced tab and then Environment Variables…
system properties
  1. In the Environment Variables window, under System variables, select Path, then Edit…
environment variables
  1. Search the list for C:\Program Files\nodejs. If this path doesn’t exist, you need to create a new one.
edit environment variable
  1. Click New…, then input a Variable name. For the Variable value enter “C:\Program Files\nodejs“. Now click OK

3. Update Node.js

Outdated versions of Node.js can cause serious issues. To upgrade Node to the latest version:

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

Linux Operating System

There are many Linux distributions available, but for the purpose of simplicity, this article will focus on solutions for Ubuntu. Ubuntu boasts the largest Linux distribution market share and has been around since 2004. Other popular distributions include Manjaro, Linux Mint, and Red Hat, but much of the solutions will be applicable to these distributions as well. The solutions are specifically for Ubuntu 20.04.

Below are four solutions to solve the ‘npm command not found’ error on Ubuntu:

1. Install Node.js and npm

Installing Node.js and npm on Ubuntu is fairly simple:

  1. Open a terminal window pressing CTRL+ALT+T simultaneously. Alternatively you can navigate to the Activities menu and search terminal, then clicking on the resulting icon for terminal.
  2. Type the following commands:
    • sudo apt update
    • sudo apt install nodejs npm
  3. When the packages are finished installing, you can verify that both Node.js and npm are installed by entering:
    • node –version
    • npm –version

In this solution, the sudo command gives the user elevated permissions (the same as an administrator). Now that Node.js and npm have been installed and verified, you can attempt to run an npm command.

2. Check PATH and system variables

You now know that npm is correctly installed on your system. Next we will verify the default path for the npm command:

  1. Open a terminal window pressing CTRL+ALT+T simultaneously. Alternatively you can navigate to the Activities menu and search terminal, then click on the resulting icon for terminal.
  2. To check the current path for npm commands enter “which npm“.
  3. The /etc/environment file needs to be checked for the file path from above, which may look something like /usr/bin/npm. Enter “cat /etc/environment“.
  4. This will give you a readout of your environment variable. You want to find the same file path from step 2, in your environmental variable. If not, continue on to add the npm path.
  5. To edit the /etc/environment file enter “vi /etc/environment“.
  6. Now you need to add a colon(:) and the npm path from step 2 to the end of the environmental file. This path file should end at ‘bin’. Make sure this path is before the end of the quotes.
  7. Enter “source /etc/environment” to reload the file with the changes.

3. Give User Permissions to Run Commands

Linux has been designed to be a secure operating system, because most applications have very limited permissions. Sometimes this can cause interference with how programs work. To give yourself permissions to edit node modules (where npm is located):

  1. Open a terminal window pressing CTRL+ALT+T simultaneously. Alternatively you can navigate to the Activities menu and search terminal, then click on the resulting icon for terminal.
  2. Enter “sudo chown -R $(whoami):admin /usr/local/lib/node_modules/“.
  3. Attempt to run an npm command and see if the problem reoccurs.

4. Update Node.js

Finally, older outdated versions of Node.js can cause version issues. To upgrade Node to the latest version, you will use npm:

  1. Open a terminal window pressing CTRL+ALT+T simultaneously. Alternatively you can navigate to the Activities menu and search terminal, then click on the resulting icon for terminal.
  2. Enter “sudo npm cache clean -f” to clear your current cache so that there are no problems accessing the correct file path, if you changed it in the second solution.
  3. Now enter “sudo npm install npm@latest -g“.
  4. You can verify that Node updated to the latest version by entering “npm -v“.

MacOS

1. Install Node.js and npm

Homebrew is a highly popular package manager for MacOS. If you have Homebrew installed on your Mac computer, it is very easy to install Node.js. You should strongly consider downloading Homebrew if you don’t have it. This solution will show you how to install Homebrew, as well as Node.js and npm:

  1. Click the Launchpad icon in the Dock, then search for Terminal. Click the result for Terminal.
  2. Type in the following command into your Terminal window to install Homebrew:
    • /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
  3. Then enter:
    • brew update
    • brew install node
  4. To verify that both are installed, enter “node -v” and “npm -v” to confirm the version of both Node.js and npm.
  5. Check if the ‘npm command not found’ error remains.

Alternatively you can find and use the Node.js installer for macOS.

2. Check PATH and system variables

For macOS, npm is usually found under /usr/local/bin. To check your PATH variable:

  1. Click the Launchpad icon in the Dock, then search for Terminal. Click the result for Terminal.
  2. Into the terminal enter echo $PATH, to display your PATH variable.
  3. Now enter “npm get prefix“. Compare the two outputs. Most likely the npm get prefix command will report /usr/local/bin. This should also be found in your PATH variable.
  4. If the prefix is NOT found in the PATH variable, it needs to be set. To set your PATH enter “export PATH=”[prefix]/bin:$PATH” >> ~/.bash_profile“. [prefix] is the value returned in step 3. Now enter “source ~/.bash_profile“.
  5. Check to see if the ‘npm command not found’ error is now resolved by trying the same command that failed previously.

3. Give User Permissions to Run Commands

MacOS shares a lot of similarities with Linux. Like Linux, you may need to give yourself permissions to edit node modules to bypass this error:

  1. Click the Launchpad icon in the Dock, then search for Terminal. Click the result for Terminal.
  2. Enter “sudo chown -R $(whoami):admin /usr/local/lib/node_modules/“.
  3. Attempt to run an npm command and see if the problem reoccurs.

4. Update Node.js and npm

If you installed Homebrew from the first MacOS solution, this will be extremely simple. If you don’t have Homebrew or another package manager like Nvm or N, you need to visit the Node.js website to find the newest installer for the updated version. Make sure to follow the default installation settings and don’t choose custom install unless you know what you are doing.

  1. If you have Homebrew, click the Launchpad icon in the Dock, then search for Terminal. Click the result for Terminal.
  2. Enter “brew upgrade node“.
  3. That’s it! Double-check by entering “node -v” and “npm -v” to ensure you are up-to-date.

Key Takeaway

The ‘npm command not found’ can be a major pain when you are working to develop an application and can’t access an important directory like npm. Without npm you don’t have access to the libraries for Node.js, and will spend significant extra time coding simple and common features by hand.

This article was broken down into three major sections for the operating systems Windows, Linux, and MacOS. This was to allow you easy access to solutions that would be helpful for your system, without requiring you to wade through non-useful information.

The four primary causes of the ‘npm command not found’ error are Node.js and/or npm not installed, path and system variables not set properly, outdated Node.js, and permissions conflicts. The solutions across all three operating systems are fairly simple and straightforward. If you were not familiar with command line (terminal, bash, etc) commands, you may have learned some useful commands in these solutions. Let us know in the comments section which solution worked, or if we have left out anything!

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 –

  • 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
  • Keytool command issues
  • Cmake command errors

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

I have installed node.js in my computer. I can check the node and npm version in my cmd. I am using laravel homestead with vagrant in windows 10. however. When I use git bash the node version is shown buy the npm is not recognized returning a message saying npm: command not found.

I do appreciate any help.

Uwe Keim's user avatar

Uwe Keim

39.7k57 gold badges175 silver badges291 bronze badges

asked Aug 4, 2017 at 18:34

dina's user avatar

Assuming you have both Git and Node.js with npm installed on your system, make sure that git and node are installed within a same directory of your Application. Eg:

App
|-git
| |-git_bash.exe
| |-etc
| |-profile
| |-node_env.conf
|-node
|-node
|-npm

Make sure node and npm are inside the node directory in App. Look at the example tree above.

open git bash and type:export PATH=$PATH:/c/App/node

After exporting the node directory in your App, now check if the npm is installed by: npm --version.

If you want to configure this when launching your terminal, open the node_env.conf file and pass in the node directory path by: PATH=$PATH:/c/App/node

Now open the profile git bash file at C:\App\git\etc\profile and add source "etc/node_env.conf"

Now go ahead and clone some git repo and test it.

git clone your_git_repo
npm install
npm run watch

answered Aug 4, 2017 at 19:15

eyoeldefare's user avatar

eyoeldefareeyoeldefare

2,1561 gold badge15 silver badges25 bronze badges

  1. The error above happens when the Windows operating system doesn’t
    know what to do with the npm command.

  2. To fix the error, you need to make sure that the Node executable
    file is available under your PATH setting.

  3. Note: NodeJS should have added the executable location to your PATH
    variable during installation process, but sometimes it may fail to
    do so.

You can add new entry to the Windows PATH environment variable using the Command Prompt as follows:

> SET PATH=C:\Program Files\Nodejs;%PATH% 
> npm 

answered Aug 12, 2021 at 20:50

Muhammad Laraib Khan's user avatar

Introduction

npm is the default package manager for Node.js, the popular runtime environment for executing JavaScript code. The npm package manager consists of a command-line client, also called npm, and an extensive online repository, the npm registry.

Developers use the npm client to access and search the npm registry for JavaScript resources, install and manage packages, and handle versioning and updates for dependencies.

Learn how to fix the npm: command not found error in Windows or Linux and ensure your Node.js projects stay on schedule.

Instructions on how to fix the npm command not found error in Windows and Linux.

Prerequisites

  • Windows: Access to the command prompt (CMD) or PowerShell and a user account with administrator privileges.
  • Linux: Access to a command line/terminal window and a user account with sudo or root privileges.

What Causes the «npm: command not found» Error?

The npm: command not found error indicates that the npm command-line interface (CLI) is not recognized or properly installed on your system.

The npm command not found error in a terminal window.

The main causes of the npm: command not found error include the following:

1. The npm package manager is not installed.

2. The npm PATH and System Variables settings are incorrect.

3. Conflicting Node.js installations on the same system or an outdated Node.js version.

4. The user executing the command does not have the necessary permissions.

Troubleshooting «npm command not found» on Windows

To fix the npm: command not found error on Windows:

  • Ensure that npm and Node.js are installed correctly.
  • Check the PATH and system variables in Windows.
  • Remove conflicting Node.js installations.

Check if npm is Installed on Windows

To confirm npm is installed, access the Windows Command Prompt or PowerShell, and use the following command to check the npm version:

npm -v

If npm is installed, the terminal displays the version number.

The npm version in the Windows cmd.

If you receive the npm: command not found error, you need to install Node.js and npm.

Note: The npm client is part of the default Node.js package.

To install Node.js and npm on Windows:

1. Visit the official Node.js page and download the latest Node.js Windows Installer.

Downloading the Windows Installer for Node.js.

2. Access the download location and double-click the Windows Installer Package.

3. Select Next on the initial Node.js Setup Wizard screen.

The Node.js setup wizard in Windows.

4. Accept the License Agreement and click Next.

Accept Node.js license agreement.

5. Set the destination folder for the installation and select Next.

Setting up the Node.js installation folder in Windows.

6. Ensure that the npm package manager is part of the installation bundle. Click Next to proceed.

Ensure npm package is part of the Node.js installation bundle.

7. The tools for compiling native modules are not mandatory and require an additional 3 GB of space. Check the box if you would like to install these tools and select Next to continue.

Install additional Node.js tools.

8. Click Install to start the installation process.

Installing Node.js on Windows.

9. Select Finish to exit the Setup Wizard once the installation is complete.

Finish Node.js installation in Windows.

10. Access the Windows Command Prompt (or PowerShell) and check the Node.js version:

node -v

Checking the Node.js version in the Windows cmd.

11. Verify the npm version:

npm -v

The npm version in the Windows cmd.

Check Path Variables on Windows

The system may not recognize the npm command due to incorrect or missing environment variables. Follow these steps to check and configure environment variables in Windows:

1. Type environment variables in the search box on the Windows taskbar.

2. Open the Edit the system environment variables panel.

Accessing the Path variables in Windows.

3. Expand the Advanced tab and click Environment Variables.

Access environment variables in Windows.

4. Select Path in the System Variables section and click Edit.

Editing system variables in Windows.

Note: If the Path variable does not exist, click New to create a new Path variable, and add the npm installation directory path.

5. Check if the environment variables list contains the path to the npm installation directory. The default path is C:\Program Files\nodejs.

6. Configure a new path or edit the existing npm directory path if you have installed npm in a different directory.

7. Click OK to save the changes and close all windows.

Edit npm environment variable in Windows.

Restart the command prompt for the changes to take effect.

Remove Conflicting Node.js Installations

Multiple installations of Node.js or npm on the same system can lead to conflicts and the npm: command not found error. To prevent potential issues, remove conflicting installations and keep only the desired Node.js version.

Use nvm (Node Version Manager) if you need to run and manage multiple Node.js versions on a single machine. The nvm tool enables users to update Node.js versions and downgrade Node.js with a few simple commands.

Troubleshooting «npm command not found» on Linux

Complete the following steps to troubleshoot the npm: command not found error in Linux:

1. Confirm that npm is installed.

2. Check the npm installation directory PATH and system variables.

3. Check user permissions.

Check if npm is Installed on Linux

To confirm if npm is installed, access the terminal window, and check the npm version using the following command:

sudo npm -v
Checking the npm version in Ubuntu.

If you receive the npm: command not found error, install Node.js and npm using the instructions for your Linux distribution.

Install npm on Ubuntu/Debian

1. Update the apt package lists using the following command:

sudo apt update
Updating apt package lists in Ubuntu.

2. Use the apt command-line tool to install npm:

sudo apt install npm -y
Command to install Node.js and npm in Ubuntu.

Note: If Node.js is not installed, the system proceeds to install both Node.js and npm from the Ubuntu repository.

3. Once the installation is complete, type npm to view the command Usage page:

npm
The npm usage page in Ubuntu.

The usage page contains essential commands and information, including the npm version you installed.

Install npm on Rocky/Fedora

Note: Installing Node.js and npm from the official repository of your Linux distribution has the fewest steps but may result in installing an older Node.js version. Use the Node Version Manager (nvm) to install the latest Node.js version on CentOS and other RHEL-based distributions.

1. Enter the following command to install npm on RHEL-based Linux distributions like Fedora or Rocky Linux:

sudo dnf install npm -y

Note: If Node.js is not installed, the system proceeds to install npm, Node.js, and Node.js dependencies.

Installing Node.js and npm on Fedora.

The Complete! message indicates that npm is installed.

Use the which command to view the npm installation directory path:

sudo which npm

The npm installation directory path in Fedora.

Check Path Variables on Linux

Enter the following command to check if the npm installation directory is in the system’s PATH:

echo $PATH

The output must contain the path to the npm installation directory. The default npm installation directories are /usr/bin/npm or /usr/local/bin/npm.

The PATH variables in Ubuntu.

If the npm installation directory path is not present, update the PATH to include the directory where npm is installed.

To set the system’s PATH environment variable in Linux:

1. Use a preferred text editor, in this example, nano, to access the .bashrc file:

sudo nano ~/.bashrc

2. Append the following line at the end of the .bashrc file:

export PATH="$PATH:/path/to/npm"
Path to npm in the .bashrc file.

Replace /path/to/npm with the npm installation directory path on your system.

3. Save the file and close the text editor.

4. Enter the following command to apply the changes:

source ~/.bashrc

5. Restart the terminal to complete the PATH update.

6. Use the echo command to confirm the new PATH variable has been added:

echo $PATH
Adding npm path to PATH system variable.

Check Permissions on Linux

The system may return the npm: command not found error if the user lacks the appropriate permissions for the npm installation directory.

npm creates the node_modules directory by default to track locally installed packages via the package.json file.

Use the chown command to give your user the necessary permissions for the node_modules directory:

sudo chown -R $(whoami):root /path/to/node_modules

Replace /path/to/node_modules with the node_modules path on your system.

Conclusion

You have successfully resolved the npm: command not found error and can continue using npm to install and manage Node.js packages.

Find out how the package.json file works and use it to automate the management of Node.js package dependencies.

How to fix the NPM command not found error?

List of content you will read in this article:

NPM is a top-rated package manager for Node.js applications. However, in some cases, a user may find trouble installing it or, by any chance, finds it difficult to resolve the “npm package missing” error. This article will go through different solutions that can help you fix this common issue quickly.

What is NPM

NPM, or Node Package Manager, is a NodeJS package manager. A package manager allows you to specify the dependencies for your project, stating that this tool or site requires these specific third-party components in order to run (or develop). It will then figure out what they need and download it for them automatically. The following are major advantages of using the npm package manager:

  • It deals with recursive dependencies.
  • The dependencies can be re-assembled as needed and do not need to be stored in your repository.
  • You can easily specify and change the version of these packages on which you rely.

What Causes the NPM command not found Error?

The «npm command not found» error typically occurs when the Node.js package manager (npm) is not properly installed or its executable is not added to the system’s PATH environment variable. Here are a few possible causes for this error:

Node.js not installed

If Node.js is not installed on your system, it will not include npm by default. You need to install Node.js, which includes npm as a package manager.

Incomplete installation

If Node.js is installed but npm is missing, it may be due to an incomplete installation. In some cases, when you download Node.js, it might exclude npm or encounter errors during the installation process.

Incorrect PATH configuration

The PATH environment variable is a list of directories where the operating system looks for executable files. If the directory containing the npm executable is not included in the PATH variable, the system won’t be able to locate and execute npm.

Operating system-specific issues

On some operating systems, there can be additional configuration steps required to ensure npm is accessible. For example, on macOS and Linux, you may need to use a package manager like Homebrew or nvm (Node Version Manager) to install Node.js and npm.

Corrupted installation or conflicts

In rare cases, a corrupted installation or conflicts with other software on your system can lead to the «npm command not found» error. This can happen if there are multiple installations of Node.js or if there are conflicts with other package managers.

Solutions to the “npm command not found” error

This is a standard error that people face in any Operating System. There can be many reasons for this error: system or path variable has been modified, installation mistake, permission issue, etc. Let’s look at possible fixes for this error.

Solution 1 on Windows

Go to this website and download Node.js. Select “Windows Installer” with the “.msi” extension and choose your suitable architecture from 64 or 32-bit options. 

Once Node.js has been installed, the installation procedure will start. Accept the terms and click on Next.

The following dialogue will ask you for the location of the installation of Node.js. Choose any location that suits you.

The following window will ask you to install additional tools along with Node.js. Leave it as the default unless very necessary.

The following window will ask you to install Chocolatey along with Node.js. Again, leave it as the default unless very necessary.

The following window will ask you to start the installation. Click on Install. The installation will then begin.

After installation, verify the npm installation using the following command

> npm -v

Solution 2 for Windows

The path variables are by default added in Windows while installing Node.js. However, some other processes can cause it to get removed or modified. 

To fix this, go to Edit the system environment variables from the Start menu.

 

A new dialogue box saying System Properties will appear. Click on the Environment Variables option from it. 

Select the option named Path from the list and click on Edit.

If the path for Node.js is not present, click on New and paste the location of the Node.js folder. In our case, it was “C:\Program Files\nodejs.”

Solution 1 for UNIX-like systems

For Linux users, type the following sequence of commands to install npm and Node.js

$ sudo apt-get update

$ sudo apt-get install nodejs

$ sudo apt-get install npm

Solution 2 on UNIX-like systems

There can be permission issues in UNIX-like systems. 

To give the required permission to the user, type the below commands on respective Operating Systems and press Enter.

Mac:

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

Linux:

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

Solution 3 on UNIX-like systems

You might also not be using the latest version of npm. Run the following command to upgrade npm:

$ sudo npm install npm@latest -g

Conclusion

This article went through various solutions for the “npm command not found” error. We saw how we could install it from scratch and configure some settings if it is already installed and fix this issue. We covered aspects like permission issues, installation issues, path variable issues to fix this error.

People also read:

  • How to install Node.js and NPM 
  • How to update the NPM package manager
  • How to check the NPM version

  • Node js javascript runtime что это за процесс windows
  • No volume label при установке windows
  • Node js запуск сервера windows
  • No bootable medium found virtualbox windows
  • No supported authentication methods available server sent publickey putty windows