Homebrew & NPM Error: ‘npm’ is not Recognized as an Internal or External Command

Updated: January 29, 2024 By: Guest Contributor Post a comment

Overview

Developers using Node.js often encounter an error stating that npm is not recognized as an internal or external command. This tutorial will explain the common reasons behind this error and provide various solutions to resolve it.

Understanding the Error

The error message ‘npm’ is not recognized as an internal or external command usually indicates that the system’s Command Prompt or terminal cannot find the npm executable in the system PATH.

Reasons for the Error

  • Incorrect PATH Environment Variable: The npm executable path is not included in your system’s PATH environment variable.
  • NPM/Node.js Installation Issue: npm may not be installed correctly on your machine, often due to a corrupted Node.js installation.
  • Conflicts with Existing Software: Other software installed may conflict with npm’s execution.

Solution 1: Verify and Install Node.js

Ensure that Node.js and npm are properly installed on your system as npm comes bundled with Node.js.

  1. Visit the official Node.js website and download the installer for your operating system.
  2. Run the installer and follow the prompts to install Node.js and npm on your computer.
  3. After the installation, restart your computer.
  4. Open a Command Prompt or terminal and type node -v and npm -v to check if Node.js and npm are installed successfully.

Example:

C:\> node -v
v14.15.1

C:\> npm -v
6.14.8

Notes: Installation is straightforward but ensure you have administrative privileges. Restarting the system is important to refresh the environment variables.

Solution 2: Add NPM to PATH

Manually add the location of the npm executable to your system’s PATH environment variable.

  1. Find the path where Node.js is installed. By default, it is usually C:\Program Files\nodejs\.
  2. Right-click on ‘This PC’ or ‘My Computer’ on desktop, select ‘Properties’, then click on ‘Advanced system settings’.
  3. In the System Properties window, click on the ‘Environment Variables…’ button.
  4. In the Environment Variables window, under ‘System Variables’, scroll down and find the ‘Path’ variable, then click ‘Edit…’
  5. Click ‘New’ and add the path where Node.js is installed, then click ‘OK’. For example, C:\Program Files\nodejs\.
  6. Click ‘OK’ to close each open dialog, then restart your Command Prompt or terminal and test if npm is recognized.

Notes: Must have administrative access to change environment variables. This fix is local to the machine and will need to be applied to each affected system.

Solution 3: Reinstall Node.js Via Homebrew

For Mac users, it might be helpful to reinstall Node.js via Homebrew, which is a package manager that helps to manage software installations on macOS.

  1. Open the Terminal application.
  2. If Homebrew is not already installed, install it by running /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)".
  3. Run brew uninstall node to uninstall Node.js and npm.
  4. After the uninstallation is complete, clear the cache with brew cleanup.
  5. Install Node.js and npm again by running brew install node.
  6. Once the installation is complete, restart your terminal and test with node -v and npm -v.

Example:

$ brew uninstall node
Uninstalling /usr/local/Cellar/node/14.15.1... (1,160 files, 24MB)

$ brew cleanup
Removing: /Users/username/Library/Caches/Homebrew/node--14.15.1... (7.9MB)

$ brew install node
==> Downloading https://homebrew.bintray.com/bottles/node-14.15.1.catalina.bottle.tar.gz
==> Pouring node-14.15.1.catalina.bottle.tar.gz 
... Output truncated ...

$ node -v
v14.15.1

$ npm -v
6.14.8

Notes: Using Homebrew ensures that dependencies are properly managed, and any existing conflicts are likely to be handled. Make sure your Homebrew is up to date before proceeding.