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.
- 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.
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 following is an overview of the main features and advantages of using
npx instead of
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 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.
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