What is NPX?

JavaScript is definitely one of the most interesting, popular, and busiest languages out there.

So, whenever there’s a new buzzword flying around in programming, chances are it is JavaScript related. One such buzzword is NPX.

At this point, I’d love to briefly explain what Node and NPM are so as not to get confused.

Node

A JavaScript runtime that enables us to run javascript outside of a browser. It also enables us to run Javascript on the server-side.

NPM

Stands for Node Package Manager and is a tool that allows us to install and manage node packages as dependencies.

So, what is NPX?

NPX is an NPM package runner that makes it really easy to install any sort of node executable that would have normally been installed using NPM.

Why use NPX?

There are a number of ways to install node packages, you can have them sitting locally (local to the project) or install globally (in the user environment).

Sometimes, instead of using either of the two install methods above, you may just want to use the package and go.

Sometimes, you might just want to experiment with a list of packages as you may not know exactly what you need.

In these cases, ​instead of installing locally or globally, you can go straight to running those packages with NPX.

How does it work?

NPX comes bundled with NPM starting with version 5.2+. So, if your version of NPM is 5.2 or higher, then you have NPX installed.

When you run a package using NPX, it searches for the package in the local and global registry, and then it runs the package.

If the package is not already installed, NPX downloads the package files and installs the package, but it will only cache the files instead of saving it.

To use NPX, you would run a command like this:

npx some-package

One great way for you to see how quickly NPX works is to create a react app using:

$ npx create-react-app my-app

The above command will generate a react app, named my-app, in the path that the command was run in using the create-react-app package. NPX then searches for the package in your environment. If it is not found, NPX downloads the files and runs the command to create a new react app, using just that one line of command.

One disadvantage of NPX is that it needs to search for packages, whether or not they are installed, before it actually runs them. This, to me, can sometimes be an overhead when you need to get things done very quickly.

Thank you so much for reading, I hope I have been able to at least shed some light on what NPX is, to explore further you can read here.

Free Resources