NPM is a well-known package manager for Node, while NPX (Node Package eXecute) is a lesser-known toolkit that was added to NPM in version 5.2.0.
In this post, we will briefly discuss NPX, its usage, and how to install NPX in Linux distributions.
What is NPX?
NPX is defined as the executor of NPM packages, it automatically installs dependent packages and executes a command, without the need to install the package first.
For instance, to create a React project with create-react-app scaffolding, you would generally need to install create-react-app before using the command to create the project. However, with npx, you can omit this step.
Usage of NPX
Using NPX is easy, for example, to use NPX with create-react-app you can simply type the command:
$ npx create-react-app my-react-app
The above npx command will look for executable commands in the ./node_modules/.bin directory of the current directory. If it is not found, it will search for the corresponding module installed globally, and if it is not found, it will automatically download the corresponding module, like create-react-app.
NPX will download create-react-app to a temporary directory, delete it when it is finished, and will not occupy local resources.
Using NPM you will need first to install the tool and then use it to create a new Reactjs project (so we need to install the tool first otherwise it will prompt with an error saying command not found).
Install NPX in Linux Distributions
NPX comes with NPM, so we first need to install NPM to use NPX. To install the latest version of NodeJS, we recommend using NodeSource, which provides many different Node versions starting from version 14 to the latest version, which is 19 at the moment.
Install NodeJS on Ubuntu
$ curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash - && $ sudo apt-get install -y nodejs
Install NodeJS on Debian
$ curl -fsSL https://deb.nodesource.com/setup_19.x | bash - &&\ $ sudo apt-get install -y nodejs
Install NodeJS on RHEL-based Distros
For RHEL, CentOS, Rocky, AlmaLinux, CloudLinux, Amazon Linux, or Fedora, run the following command as root:
$ sudo curl -fsSL https://rpm.nodesource.com/setup_19.x | bash - $ sudo yum install -y nodejs
To check if you have installed NodeJS and NPM correctly, run the commands:
$ node -v $ npm -v
After installing NPM, we can install NPX by running the commands.
$ npm i npx $ npx -v
NPX Parameters Usage
There are several parameters that can be used with NPX and they are:
--no-install
tells NPX not to download automatically, which means that subsequent commands cannot be executed if the module is not available locally.--ignore-existing
tells NPX to ignore existing modules and perform a download operation every time, that is, it will download and install temporary modules each time and delete them when they are finished.-p
is used to specify the modules to be installed by NPX, which can specify a certain version to install.-c
tells NPX that all commands are interpreted in NPX.
Conclusion
In conclusion, NPX is a tool that comes with NPM since version 5.2.0 and serves as an executor for NPM packages.
It allows users to execute a command without the need to install its dependencies. In this post, we have installed NodeJS, NPM, and NPX, and also learned about different parameters that we can use with NPX.