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 NPX is, the difference between NPM and NPX, its usage, and how to install it on major Linux distributions.
|NPX (Node Package eXecute)
|Root or Sudo Privileges
|Ubuntu, Manjaro, Fedora, etc.
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, you generally 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 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 (learn more).
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 “
Note that the packages that 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 on 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 in 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 on 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 on 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 on an 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 the React app example, so let’s see a few more examples of the usage of NPX to make it easier for you to understand its use case.
1. 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
2. You can force NPX to use the local packages and not download them if they are not found using the “
If the referenced package does not exist locally, an error will be reported.
$ npx --no-install http-server
3. Instead of using the local packages (if they exist), if you still want to download them, you can use the “
It will download the referenced packages even if they are present locally.
$ npx --ignore-existing create-react-app my-app
4. Multiple packages can be easily installed using the “
-p” flag, as shown.
$ npx -p lolcatjs -p cowsay [command]
5. The “
-c” flag brings the NPM environment variables into the NPX command.
$ npx -c 'echo "$npm_package_name"'
6. Execute the binary from a given NPM module, suppressing any output from NPX itself.
$ npx --quiet create-react-app my-app
7. 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, it’s unrecognized 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!