Select Page

Introduction

NPX is short for “Node Package Execute”, and is a command line program used to run npm packages without even needing to install them. It is not recommended to use it for applications you’d want to run on a regular basis, but only when you need to execute them once or twice on occasions. However, at times it is actually quite handy if you have this command installed.

Unfortunately, some users have reported to have encountered the following error message in their terminals when they try to execute the npx program:

npx: command not found

In this article, we will thoroughly be discussing the fix for this issue, so that you could run programs using npx without any further problems.


Why is the npx command not found?

The error message states that the npx command cannot be traced by your system when you’re asking for its execution. The reason for this might be that you haven’t installed the command in the first place. Yes. If you had thought that it comes shipped by default when you install npm, you’d be wrong. Besides that, the other most common appearance of the problem is after installing npx locally.

Using npm, you can install packages both locally and globally. By default, when you run npm install followed by a package name, it installs the package in your local folder, the one you’re currently located in. If a package has been installed locally, it can be executed only from the directory where it is installed, and nowhere else.

The following sections will discuss how to fix this and install npx properly on your Linux machine.


Install npm in Linux for npx

Before you even proceed on to installing npx, you should make sure that npm has properly been installed, and your terminal is able to run it:

$ npm -v

If the terminal gives you a proper output with a version number, then you’d be good to go. If it doesn’t, and rather returns another new error, you need to first install npm.

Installing npm in a Linux distribution requires you to use a package manager, which will be different based on the package manager you’re using. Some of the commands required to install npm in popular distributions are listed below:

Arch Linux

$ sudo pacman -Syu
$ sudo pacman -S npm

Ubuntu

$ sudo apt-get update
$ sudo apt-get install npm

Debian

$ sudo apt-get update
$ sudo apt-get install npm

Fedora

$ sudo yum makecache
$ sudo yum install npm

RedHat

$ sudo dnf makecache
$ sudo dnf install npm

CentOS

$ sudo dnf makecache
$ sudo dnf install npm

The first line of command is used to update your system repositories. This is very important as otherwise, you might be unable to install any packages in your machine. The second line of command installs the npm package.


Install npx globally using npm

Once you’re properly installed npm and checked whether it works fine, you can now proceed to installing npx, globally. 

To install npx globally using the Nodejs Package Manager (npm), you’d use the following command:

$ sudo npm install -g npx

The -g flag directs npm to install the target package globally, so that you can execute it from any directory you might be in. 

Once installed, you should check whether it works by running:

$ npx -v

If the command runs properly, it should return you with a version number.


Fix your PATH variable

So far so good. But unfortunately, many people might still be unable to run the npx command on their machines despite having installed it properly. Why is that, you may ask?

As it appears to be, the directory where all the globally installed packages of npm are stored, may not be listed in your system’s global PATH variable. This environment variable is responsible for listing the directories which have commands, programs, scripts and all sorts of other stuff in your system that can directly be executed from a terminal, from any given directory. If a directory contains some programs or scripts, which is not listed in the PATH variable, then you’d be unable to run that script or program from any other directory than the one where it is stored, similar to the case of locally installed packages of npm.

Npm stores all the globally installed packages under the “/usr/local/lib/node_modules” directory in your system. So if for some reason, this directory is not listed in the PATH variable, then you need to manually include it first. 

To do this, navigate to your home directory and create a file called .profile (it may already exist in that directory. If it does, don’t recreate it). Open up this file using your text editor of choice. I’d use nano:

$ nano ~/.profile

Once inside this file, append the following line of code:

export PATH=$PATH:/usr/local/lib/node_modules

Save and exit the file, and restart your terminal for changes to take effect. Now try running the npx command once again. If it successfully works, congratulations!

You’ve successfully eradicated the error message regarding npx not being found in your system.