How to fix npm command not found error

When you try to run npm commands such as npm install, there may be times when the Terminal would respond with npm command not found error as shown below:

$ npm install -g n
zsh: command not found: npm

The command not found error occurs when your computer can’t find the associated program on your computer’s PATH environment variable.

The PATH environment variable is a list of directories specifying where your computer should look for a program to run from the Terminal.

For example, when you execute the npm command from the Terminal, your computer will look through all the folders listed under PATH to find the npm executable file and pass your commands to it.

When the npm executable file is not found, then your computer will respond with the command not found error above.

When you install or update your npm version, the folder where the npm executable file should be automatically added to your computer’s PATH environment variable.

The reason why your npm command can’t be found might be that the PATH environment variable is somehow misconfigured.

To fix this error, you need to add the folder where the npm executable file is located back to the PATH variable.

But to do this, you need to know where your npm executable file is located first.

For Windows users, the npm executable file may be located inside C:\Program Files\nodejs

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

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

Or you can set the PATH variable by using Windows graphical UI.

The following steps will help you to fix the error from the Windows interface.

Fixing npm command not found error on Windows OS

First, Go to My Computer or This PC for Windows 10. Right-click on empty space and open the Properties window:

Click Advanced system settings from the left bar of the Properties window:

Now you’re in the System Properties window. Click the Environment Variables... button:

Now you’re in the Environment Variables window. Select the Path variable from either the User Variables or System Variables table and click on the Edit... button:

At the end of the variable values, add C:\Program Files\nodejs as a new entry to your Path variable:

If you installed NodeJS on another location, then you need to change the entry to your custom location. Remember to always enter the absolute path from the drive letter to your NodeJS program folder.

Once done, click OK and open a new Terminal or Command Line window. If you’re calling npm from the VSCode terminal, you need to restart VSCode first before trying again.

The npm command not found error should be fixed and you should be able to check this using the npm --version command:

> npm --version
7.15.1

If you still get the error, then try restarting your computer first. The error should be gone after you restart.

Now you’ve learned how to fix the error on Windows, it’s time to fix the error on macOS or Linux system.

Fix npm command not found error on macOS or Linux

The npm executable file is usually installed under /usr/local/bin/ for macOS and Linux systems, so you need to see if the location is already added to your PATH variable using the echo command as shown below:

$ echo $PATH
:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

The echo output above is shortened for clarity.

Through searching online, I’ve found that most Linux and macOS systems already have /usr/local/bin/ under their PATH variable.

If you already have the folder under the PATH variable but still facing command not found error, then I’d recommend you do a clean install of NodeJS and npm.

First, you need to uninstall npm using the same method you installed it. If you use a package manager like Homebrew, then use brew uninstall node to remove it.

Then you can install your NodeJS and npm using brew install node command.

After a clean install, you should be able to check the NodeJS and npm versions installed on your computer:

$ node -v
v16.3.0
$ npm -v
7.15.1

Now you should be able to do npm install again. Great job on fixing the error!

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.