What is NPX and why would we use it?

I'll attempt to explain what NPX is and give an example of when or why we'd use NPX over NPM. Find out more.

What is NPX and why would we use it?

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?

Before we explore the differences, we need to look at NPM to understand how it's different to NPX. NPM is an acronym for "Node Package Manager". It's a package manager chiefly for adding dependencies to a JavaScript or Typescript project. Using the '-g' flag, you can also install packages globally.

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.

Introducing NPX

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:

npx @nestjs/cli
What is 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.