How to Fix NPM Command Not Found on Windows, Linux, and Mac OS

How To Fix Npm Command Not Found

A path in operating system is a link to various programs installed in it. There are different path location, mainly general path and customized path. For general path, any operating system automatically install a program into centralized directory for example /usr/bin. If we are installing from source, there are options to put our program outside central directory. When this happen, we would find message like any-program command not found.

The ‘not found’ message always identic on any operating system. To fix this, we need to set environtment variable.

Setting Path in Windows OS

To set a path in Windows, open comand prompt and type the following command:

set PATH=%PATH%;D:\directory\program\program-name\

Setting Path in Linux and Mac OS

To set path in Linux OS, open terminal and type the following command:

export PATH=$PATH:/diretory/program/executable-file

NPM Installation

Npm Developer
Npm Developer

In any operating system, when a program has not been installed yet, it will return 'command not found' when we run it. Here is the example:

npm
Command 'npm' not found

To install NPM, first we need to install NodeJS. Here, we shall install Node by using NVM. The steps are:

  • Install NVM
  • Install NodeJS & NPM

Install NVM

To install NVM in our system, first, open your terminal, move to /home/user directory by cd command without argument, and download the package by using wget:

cd
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

Second, exporting path. Here, we want to define first, the variable for exporting, with the following command:

export NVM_DIR="$HOME/.nvm"

After defining variable NVM_DIR, then we can export the variable:

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Then, we check the NVM version by the following command:

nvm --version

Now, we want to install the latest stable version of Node. Simply run the following command:

nvm install node

The return of the command will be like this:

Downloading and installing node v19.1.0...
Downloading https://nodejs.org/dist/v19.1.0/node-v19.1.0-linux-x64.tar.xz...
#############...

Now, we wil define to our system, that we are going to use the latest NODE, and the latest NPM. Simply run the following command:

nvm use node

The return will be like this:

Now using node v19.1.0 (npm v8.19.3)

Here is the final execution! We begin to use NPM to install. For example, we are going to install http-server globally. Here is the command:

npm install --global http-server

Now, we want to test the server. Create a directory for example www by using this command:

mkdir www

Create a file inside the www directory:

nano www/index.html

Fill the file with the following code:

<html><body><h1>NPM Works</h1></body></html>

Run http-server command:

http-server www

Now, open in your browser http://127.0.0.1:8080

If you find that it is working, then, congratulation!

Conclusion

To install NPM, the best way to do it is by using NVM. Here are the steps:

  • Install NVM
  • Install NODE and NPM
  • Use the NODE and this will automatically use the NPM
  • When installing package using NPM, we will be noticed if there is availability of the lastest available NPM

There will be no more ‘npm command not found’.

I used to be a pilot, but ended up being just a mediocre writer.

×