Quite simply, NPX is a Node package runner. It allows us to run Node applications without installing them to our project or locally.
What's the difference between NPM and NPX?
Why would we install NPM packages globally?
There are many packages that we would install globally. I'll use the NestJS CLI as an example. The NestJS CLI allows you to create a new NestJS based application. We could run something along these lines to install NestJS CLI globally:
npm install -g @nestjs/cli
Why wouldn't we install NPM packages globally?
Using our NestJS example again, we may require a package to use within our NestJS Application. This package would be installed to the NestJS project to allow us to use the dependency within our project. We wouldn't want to globally install this as it would only be used within our project.
So, let's say we want to create our NestJS project, but we don't want to go installing anything on our development system globally? Or the thing we're doing doesn't need to exist in our project? This is where NPX comes in.
How to use NPX
Yet again, using NestJS CLI as an example, we want to create a new NestJS project without installing NestJS CLI. We could just run NestJS CLI using NPX:
As you can see from the above screenshot, we have full access to the NestJS CLI without having to install a thing.
This is just a brief explanation of NPX and when or why we'd use it. If you have any questions, feel free to ask them in the comments below.