NPX : Node.js Package Runner
If you have been using NPM and have version 5.2.0 or higher installed on your machine, you have likely noticed a command called “npx.” If you have ever used create-react-app to initialize a react application, you may have used npx to run this command. It is not uncommon for developers to use npx without really knowing what exactly it means. In this blog, we will look into all nuances associated with npx, the Node.js package runner.
There exists a lot of confusion between NPM and NPX, you can read about the difference between the two here.
What is npx?
NPX is a utility that complements the experience of using packages from the npm registry.
Just as npm makes it simple to install and manage dependencies hosted on the registry, npx makes it simple to utilize CLI(Command Line Interface) tools and other executables hosted on the registry.
On all npm versions above, 5.2.0 npx is preinstalled, but you can check if you have npx installed or not by running the following command.
// This command checks for the version of NPX installed on your computer.
In case this throws an error and does not show the version; you can install NPX using NPM like this:
// This command uses NPM to install npx globally.
npm install -g npx
The primary use case of NPX is when we need to use a particular package just once. In such cases, first installing it and then executing it becomes a very redundant task. This is why NPX is a powerful tool.
NPX is extremely useful because it considerably simplifies various previously difficult tasks to accomplish with ordinary NPM. These are discussed below in great detail.
Executing One-Off Commands
You may have encountered a situation where you wanted to run a command just once. It may have been so because that is all the utility of that command is, like create-react-app, or maybe because you wanted to try a nifty new CLI. It is incredibly redundant first to install these commands globally and then use them in such cases. It is in these situations that NPX's true powers are unleashed. Given below is an example of using the fun command "cowsay." Cowsay is a command which prints a cow saying whatever argument you pass in the command.
Without NPX, we would have to install cowsay globally using NPM to be able to use it, as shown below:
Installing cowsay globally
If we try using the command without installing it, we would only receive an error message:
Using NPX, we can avoid all of this and simply use the command cowsay without installing it globally.
Using NPX to run the cowsay command.
The above example shows how NPX lets us use the command without installing it. NPX just installs it in the local cache of the bash.
Using Different Versions of Packages
Sometimes we may need to use specific functionalities from previous versions of various tools, packages, and dependencies for our project. The solution to this problem could be updating the package version installed globally using npm, but this is not the best possible way out. The issue with using this technique is that we may not want to permanently change the version and only need it once or twice. Updating the versions again and again for specific uses is a redundant way to work with things. Here again, NPX gives an elegant solution.
NPX allows us to use specific versions of packages as well. The syntax for that is npx package@version.
Run Arbitrary Code Snippets Directly
A limitation of the node package manager is that its functionalities are limited to the NPM registry. NPX, on the other hand, does not restrict you to the NPM registry. Using NPX, we can directly run code snippets just by specifying their URLs. This is very useful if we want to try Gists from Github.
An example of this can be:
Frequently Asked Questions
- What are some of NPX’s most commonly used features?
Though in this blog, we have used fun examples like cowsay to learn about NPX, NPX is widely used to initialize React projects using create-react-app. It is even used to initialize Vue projects.
- What happens if we use NPX to run locally installed commands?
In such cases, running NPX automatically finds the package in the node_modules folder, and we do not have to specify the path by finding it manually.
In this blog, we learned about NPX, the Node JS package runner. We looked at its various uses and how to use npx to make our development environment more efficient. To sum it up, we can summarise that there are two broad practical uses of npx:
- Using packages without installing them
- Using different versions of packages