You might already be aware of the popular NPM (Node Package Manager) used as a package manager for Node, while NPX (Node Package eXecute), introduced in NPM version 5.2.0 (on August 10, 2017), an NPM package runner, is quite unpopular.
In this article, you will learn what the NPX is, the difference between NPM and NPX, its usages, and how to install it in major Linux distributions.
|Description||NPX (Node Package eXecute)|
|Root or Sudo Privileges||Yes|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
|Discussed Tools in this Article||–|
What is NPX for Node.js?
Unlike the NPM, the NPX is a package runner that is used to execute Node.js packages without the need to install them first. If the required packages are not found locally or globally, it will download them from the NPM registry.
For example, if you want to create a new React app using the “create-react-app” package, then generally you need to install this package first. However, NPX can skip those extra steps for you.
Differences between NPM and NPX
NPM (Node Package Manager) is a package manager for Node.js. It can install, remove, publish, and manage packages in single or multiple projects by installing them either locally or globally.
NPX (Node Package eXecute) is a package runner that helps to execute the packages without installing them first. If the required packages are not found locally or globally, then it will download them from the NPM registry.
In simple terms, if you use NPM, you have to install the necessary packages first, but if you use NPX, you can run your application right away.
Usage of NPX
For instance, you want to create a new React app using the “create-react-app” package, which is not installed locally or globally on your system.
Instead of installing it using the NPM first, you can directly run your React app using the NPX, like this:
$ npx create-react-app my-app
The above command will search for the mentioned package locally or globally. If the package is not found, then it will download the latest version of “create-react-app” from the NPM registry and use it to create your new React app in the “my-app” directory.
Note that the packages that the NPX downloads are kept in a temporary directory and can only be used during the current runtime.
Once the application is closed, the downloaded packages will be deleted and will no longer use any local resources.
How to Install NPX in Linux
NPX comes with the NPM package, so you need to install NPM (or Node.js) to use NPX. Although, Node.js is available in most Linux repositories, you can directly install it by mentioning the package name to your default Linux package manager.
However, in this article, you will learn to install Node.js using the NodeSource, which makes it easier to install different versions of Node.js on your Linux system.
Installing Node.js in a Debian or Ubuntu System
$ curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash - $ sudo apt install nodejs -y
Installing Node.js in a Red Hat or Fedora System
$ curl -sL https://rpm.nodesource.com/setup_lts.x | sudo -E bash - $ sudo dnf install nodejs -y
Installing Node.js in a Arch or Manjaro System
$ sudo pacman -S nodejs npm
After the installation is done, use the following command to check the versions of Node.js and NPM to make sure they were installed correctly:
$ node -v $ npm -v
Now you can run the NPX command, as shown.
Examples of NPX
We already discussed React app example, so let’s see a few more examples on the usage of NPX to make it easier for you to understand its use case.
The following command will check for the “http-server” package locally or globally. If it’s not found, then the following command will automatically download the package and start a web service in the current directory.
$ npx http-server
You can force NPX to use the local package and not download them if they are not found using the “
--no-install” flag. If the referenced package does not exist locally, an error will be reported.
$ npx --no-install http-server
Instead of using the local packages (if they exist), if you still want to download them, you can use the “
--ignore-existing” flag. It will download the referenced packages even if they are present locally.
$ npx --ignore-existing create-react-app my-app
Multiple packages can be easily installed using the “
-p” flag, as shown.
$ npx -p lolcatjs -p cowsay [command]
-c” flag brings the NPM environment variables into the NPX command.
$ npx -c 'echo "$npm_package_name"'
Execute the binary from a given NPM module, suppressing any output from NPX itself.
$ npx --quiet create-react-app my-app
Execute the source code of modules on GitHub.
$ npx github:piuccio/cowsay hello
That’s all about the NPX package runner, although it’s an interesting tool but unrecognised by many programmers.
If you want to learn more about this or a related topic, then feel free to tell us in the comment section.
Till then, peace!
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.