What is NPX, and what’s the difference between NPX and NPM, and when should you use either to execute packages for your projects?

TLDR: NPX is an automation tool that makes it fast and easy for you to run the correct versions of NPM packages on different projects — without requiring you to install the packages locally or globally on your computer.

The difference between NPX and NPM

  • NPM (Node Package Manager) is a package manager, but it’s not very good at executing (running) packages.
  • NPX (Node Package Execute) is a package-runner CLI tool that is built-in to NPM (since NPM version 5.2).

Unlike NPM, NPX is great at executing packages.

Practical NPX vs. NPM example

Let’s say you want to learn how to use ReactJS, so you go to the official ReactJS website and follow their instructions.

You’ll notice that the official documentation for ReactJS recommends you to use this NPX command to install and run create-react-app:

npx create-react-app project-name

The reason is that NPX makes sure that you run the React application with the latest versions of the packages — NPM doesn’t.

This is practical because create-react-app is updated frequently — and it's generally recommended to use the latest package versions.

If you install create-react-app globally with npm install, you’ll have to manually update your project every time packages/dependencies get updated. NPX does it for you automatically.

The main features of NPX

The following is an overview of the main features and advantages of using npx instead of npm (or yarn) to run packages:

  • NPX allows you to quickly run and test Node packages without installing them globally or locally, by caching the packages temporarily on your machine.
  • NPX automatically installs missing npm packages/dependencies for you.
  • NPX automatically installs the latest version of packages in a project when you run the npx package command.
  • NPX allows you to run different versions of the same package, on different projects. For example, if one project only works with Node v 11.1, and another only works with Node v 11.5, NPX makes that process painless.

NPM doesn’t do any of those things for you, without NPX you’d have to do everything manually.

When should you not use NPX?

You shouldn’t use NPX on a project where it’s important that your NPM package versions remain the same for the time being. In other words, if you want to manually control when to update a specific package/dependency, use the npm (or yarn) command.

Has this been helpful to you?

You can support my work by sharing this article with others, or perhaps buy me a cup of coffee 😊


Share & Discuss on