How to fix “npm command not found” error using script easily

This article is for newbies or lazy developers like me. Suppose you are facing an “npm command not found” error and can’t solve it. The bash script provided in this tutorial will help you to fix it easily without much effort.

Introduction

NPM stands for “Node Package Manager“. You can install and manage Node.js application modules or packages using npm.

Why “npm command not found error” occurs?

Below mentioned are the reasons why you are getting the “npm command not found” issue in Mac or Linux operating systems-

  1. NPM and Node.js are not installed or corrupt
  2. System PATH not set up
  3. Permission issues on the npm directory

Generally above mentioned issues can be fixed easily by installing the npm and Node.js packages as well by setting up the PATH variable. This article helps you to fix the npm and node.js issues automatically in Linux and macOS.

npm command not found error

Pre-requisite to run the script

These are the pre-requisite to run this bash script.

  1. Bash shell
  2. Internet access to download Nodejs and NPM packages, if not there already
  3. Root or sudo privileges
  4. curl to download packages to be installed

This script is designed to run on Linux (Debian or RedHat based operating systems) and MacOS (Darwin) operating systems. In case you don’t have curl installed, run the following commands.

  • For Debian-based distros
$ sudo apt update
$ sudo apt install curl
  • For RedHat-based distros
$ sudo dnf install curl

Script overview

This script will do all the possible checks and fix the issue of the “NPM command not found” error in Linux or MacOS as a final outcome.

  1. Detect the existing operating system, if it’s Linux or MacOS, and continue the script.
  2. Detect the NPM and NodeJS installation.
  3. if NPM and NodeJS is installed. Print the message it’s installed and provide the complete path of installation.
  4. In case it’s not installed, run the npm and Node.js installation commands as per the operating system.
  5. If NPM is installed, look for the path variable, If it doesn’t exist or is not correctly set up based on the operating system. Then set up the PATH variable and print the command.
  6. Suppose NPM and Node js are installed. The script found the PATH setup also. Then it looks for permission issues and fixes them. Also, print the commands which helped to fix the problem.

Script link

Download the script from here. You will find the “npm-error-fix.sh” file in your download directory. You can ignore the warnings of the file is unsafe during download. It happens due to the “.sh” extension of the file.

How to run “npm-error-fix.sh” script

To run this script, follow these steps:

I executed this script on Ubuntu 22.04 for the artefacts shared in this article.

  1. Open a terminal on your Linux or MacOS system.
  2. Navigate to the directory where you have saved the script using the cd command.
  3. Ensure that the script has executable permissions. You can do this by running the command chmod +x script.sh, where script.sh is the name of the script file.
  4. Run the script by executing the command ./script.sh.
  5. Alternatively, you can run $ bash “path_to_script.sh” to execute the file without changing permissions as shown in the image.
npm command not found fix using bash script
NPM and Node.js Version

That’s all, this bash script will fix npm command not found error on your system.

If you own Windows and want to fix npm errors, follow my other article on npm errors.

Script breakdown

=====================================================================

If you are interested in the details of each section of the script, continue reading the article.

=======================================================================

Section 1- The script runs the uname command and saves it on the “OS” variable. If the output is Linux or MacOS, the script will continue else it will throw an error i.e. “This script supports Linux and MacOS operating systems only“.

#!/bin/bash

# Detect the operating system
os=$(uname)

if [[ "$os" != "Linux" && "$os" != "Darwin" ]]; then
  echo "This script only supports Linux and MacOS operating systems."
  exit 1
fi

Section 2 – Checks for Node.js and NPM packages that are already installed using “which" command. If packages are not installed, based on the operating system, it will download and install the latest version 19.x of node.js as mentioned in the script. The script uses curl to download, then yum or apt-get commands to install the package in Linux. For macOS, it uses the brew command.

Visit https://github.com/nodesource/distributions link and look for your node.js and npm version. You can change the version as per your wish, just replace 19.x with your version in the script.

# Check if NodeJS and NPM are installed
node_location=$(which node)
npm_location=$(which npm)

if [[ -z "$node_location" || -z "$npm_location" ]]; then
  echo "NodeJS and/or NPM are not installed. Insta
lling..."

  if [[ "$os" == "Linux" ]]; then
    # Install NodeJS and NPM on Linux
    if [[ -f /etc/redhat-release ]]; then
      # Install NodeJS and NPM on Red Hat-based Linux
      curl -sL https://rpm.nodesource.com/setup_19.x | sudo -E bash -
      sudo yum install -y nodejs
    else
      # Install NodeJS and NPM on Debian-based Linux
      curl -sL https://deb.nodesource.com/setup_19.x | sudo -E bash -
      sudo apt-get install -y nodejs
    fi
  else
    # Install NodeJS and NPM on MacOS
    brew install node
  fi

Section 3 – Check for installation success using node and npm location using which command and print NodeJS and NPM have been installed and print the PATH of npm and node.js.

In case of the installation fails, the script will throw the error “Failed to install NodeJS and NPM“.

# Check if installation was successful
  node_location=$(which node)
  npm_location=$(which npm)

  if [[ -z "$node_location" || -z "$npm_location" ]]; then
    echo "Failed to install NodeJS and NPM."
    exit 1
  else
    echo "NodeJS and NPM have been installed."
  fi
else
  echo "NodeJS and NPM are installed."
  echo "NodeJS is installed at $node_location"
  echo "NPM is installed at $npm_location"
fi

Section 4 – This section of the script deals with the PATH variable of npm. In case the script found the npm and node.js installation but there is an issue with PATH variable setup. It will automatically fix PATH variable issues.

# Check if PATH variable is set up correctly
if [[ "$os" == "Darwin" ]]; then
path_file="$HOME/.bash_profile"
else
path_file="$HOME/.bashrc"
fi

path_string="export PATH=\"\$PATH:$HOME/.npm-global/bin\""

if ! grep -q "$path_string" "$path_file"; then
echo "PATH variable is not set up correctly."
echo "Setting up PATH variable..."
echo "$path_string" >> "$path_file"
source "$path_file"
echo "PATH variable has been set up."
else
The final echo "PATH variable is set up correctly."
fi

Section 5 – This section covers the permission-related issues of npm directories. If the script found installation and PATH variables are set up correctly. It will look for permission issues, fixes them and prints the message permissions have been fixed.

# Check for permissions issues 
npm_global_dir=$(npm config get prefix)
if [[ "$npm_global_dir" == "/usr/local" && "$os" == "Darwin" ]]; then
echo "Permission issue detected. Running fix command..."
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
echo "Permissions have been fixed."
fi

Once all npm-related errors are fixed and you will run this script again, you will get the following output as shown in the image.

Outcome once all npm related errors are fixed and you run the script.

In case the script fails to fix the issue, then I suggest you follow my other article, which covers all 8 solutions to fix the npm command not found error using step-by-step instructions.

Conclusion

The purpose of writing this shell script is to fix all your “npm command not found” errors automatically. If you are a newbie, this script can really save you a lot of time. I encourage you to run this script on your own and provide your feedback via your comments. I will try to answer or improve this script based on your suggestions.

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!