Connect with us

Website Tutorials

Fixing ‘npm command not found’: A Comprehensive Guide

Published

on

Fixing ‘npm command not found’: A Comprehensive Guide

Node.js is a framework that allows you to leverage JavaScript for server-side scripting, instead of just client-side work in browsers. Even though other technologies are gaining ground, Node.js still retains a strong position as the tool of choice for high-concurrency applications and ones that require real-time capabilities with a lot of I/O operations – particularly those that are “single-page”, like document collaboration. The Node Package Manager (NPM) is both the name of the library of JavaScript packages, as well as the command line tool used to manage them. When you see the “npm command not found” error, it can mean a multitude of things.

In this tutorial, I’ll show you how to replicate this error on Linux, and how to determine its underlying cause.

“npm command not found” – What it Means

Here’s a screenshot of me trying to use the NPM command when the system is unable to execute it for whatever reason:

npm command not found
npm command not found

This error message will vary depending on the operating system you’re using and the kind of shell. In the above example, I’ve connected to a CloudLinux server, and I’ve artificially made the NPM command unusable to demonstrate the error. I’d previously written about how to install Node.js using EasyApache from within WHM.

The “npm command not found” and similar kinds of errors occur when the system tool isn’t able to execute the command. This can happen for a variety of reasons as shown below.

1. Node.JS Isn’t Installed

The most obvious reason for the error is that Node.js is missing from your local system. If you need to install Node.js, check out the tutorial I linked earlier. However, if you want to make it easy to develop Node.js applications, then simply installing npm isn’t enough. I suggest you also install the NodeJS selector using CloudLinux as the operating system. This is also useful if you are a reseller and want to offer Node.js as a service to your clients.

Advertisement

To replicate the “npm command not found” message (for whatever reason), you can simply uninstall Node.js.

2. Check your PATH Environment Variable

When Node.js is installed, the npm a symbolic link to the npm command is placed in the directory represented by the PATH variable. If the PATH variable is missing or configured to something else, then the system won’t be able to find where the npm command is located.

To check the location of your npm command, type the following:which npm

The above command will determine where the “npm” command is located, as shown here:

which npm
which npm

In the above screenshot, the npm command is located at:/usr/bin/

If you’re on Linux using bash, you can find out at which directory the PATH variable is pointing by running the following command from your home directory:vi .bash_profile

Advertisement

This will open the hidden file “.bash_profile” and show you if Linux is currently using the correct npm directory in its PATH definition. Here’s a screenshot of what it looks like on my system:

PATH Location
PATH Location

You can see, that for me, the PATH variable is pointing at $HOME/bin, which should be the default, and is usually the place where the symlink to npm is placed when you install Node.js. However, if someone has interfered with the installation or the PATH variable, there might be a mismatch and you can correct it. You can either change the .bash_profile file yourself or create another symlink to npm in the location where the PATH variable is pointing.

If you make changes to the file, then after saving it, you should execute the following command:source ~/.bash_profile

This will re-instantiate the PATH variable and allow Linux to use the new PATH variable instead of the old one. If the problem causing the “npm command not found” error is a defective PATH, then the above solution will fix it for you.

3. Wrong User Permissions

In a multi-user environment like Linux, one user can have access to the npm command while another user doesn’t. Generally, while installing Node.js using a package manager like yum or apt-get, it installs the package globally for everyone simultaneously and places the command in the global PATH variable so anyone can use it. It’s not like in Windows, where the system asks you beforehand, whether or not you want to install it just for yourself.

So generally, all users on a Linux system will have access to a package like nmp if it’s installed using the regular package manager. However, there are many instances where a user might not be able to use npm, even though it’s installed on the system.

Advertisement

Different PATH Variables for Users

One common reason a user might be unable to access npm is that they have a custom PATH variable compared to the default installation location for packages. I’ve already explained above how to change the PATH variable in Linux, so you can refer to that.

Local Installations of npm

Many people install Node.js from the official Node.js website, and in the process of installing it, extract the files to a local directory to which only they have access. If Node.js is installed like this instead of through the official package manager, other users won’t have access to the npm command.

So if you want to avoid all “npm command not found” errors on Linux, the best thing to do is to install Node.js using the in-built package manager like “yum” for RHEL systems and “apt” for systems like Ubuntu. These managers install the programs in standard locations and also place links to the main commands in places that are in everyone’s PATH directory. In addition, it’s also much simpler and you don’t have to worry about things like dependencies, etc.

File and Group Permissions Mismatch

Under some circumstances, the system administrator might have decided to restrict the permissions on the npm file by only allowing executions by certain groups of users. Normally, this isn’t a problem, but a group of users can be locked out if they don’t have permission to execute the npm package.

However, the above problem with permissions will throw a different error, and not “npm command not found”. Instead, the error message will be specifically related to permissions.

Advertisement

Software Manager Restricting Access to npm

Along with Node.js, you or your administrator might have installed the Node Version Manager (NVM). In a related article, I talked about how to install the Node.js selector on WHM. While NVM and the Node.js Selector are similar, the former is used in development environments and the latter is a GUI interface for web hosting control panels like cPanel or Plesk.

These managers can restrict the access of Node.js to users and require certain versions of Node.js or have other configuration issues that cause the “npm command not found” error. If you’re using a software manager like the above, ensure that they’re set up correctly and accurately specify the version of Node.js you’re trying to use.

Conclusion

As you can see, the “npm command not found” error has a wide range of causes. Some of these are general errors for any package that can’t be found, but others are specific to Node.js and you should go down the list one by one so that you isolate the cause.

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.