Sling Academy
Home/DevOps/Homebrew & NPM Error: ‘npm’ is not Recognized as an Internal or External Command

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

Last updated: January 29, 2024

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.

Next Article: Homebrew: Uninstalling a package and its dependencies

Previous Article: Homebrew SSL_connect Error: SSL_ERROR_SYSCALL in connection to raw.githubusercontent.com:443

Series: Linux Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide