npm vs npx — What’s the Difference?
If you’ve ever used Node.js, you must have used npm for sure.
npm (node package manager) is the dependency/package manager you get out of the box when you install Node.js.
It provides a way for developers to have a global and local installation of packages.
Sometimes you’d want to take a look at a specific package and try out some commands. But you cannot do that without installing the dependencies in your local
That’s where npx comes in.
In this article, we’re going to have a look at the differences between the two and how to get the best from both.
First, let’s understand what npm actually is and what we can do with it.
npm the package manager
npm is a couple of things.
First and foremost, it is an online repository for the publishing of open-source Node.js projects.
Second, it is a CLI tool that aids you in the installation, version, and dependency management of those packages. There are hundreds of thousands of Node.js libraries and applications on npm and many more are added every day.
npm by itself doesn’t run any package. If you want to run a package using npm, you must specify that package in your
When executables are installed via npm packages, npm creates links to them:
– local installs have links created at
– global installs have links created from global
bin/ directory (for example:
/usr/local/bin on Linux or at
%AppData%/npm on Windows)
To execute a package with npm you would either have to type the local path
or you can run a locally installed package by adding it into your
package.json file in the scripts section
then you can run the script using
You can see that running a package with plain npm requires quite a bit of ceremony.
Fortunately, this is where npx comes in handy.
npx the package runner
Since version npm 5.2.0 npx is pre-bundled with npm. So it’s pretty much a standard nowadays.
npx is also a CLI tool whose purpose is to make it easy to install and manage dependencies hosted in npm registry.
It’s now very easy to run any sort of Node.js based executable that you would normally install via npm.
You can run the following command to see if it is already installed for your current npm version:
if not, you can install it like this
Once you make sure you have it installed, let’s see a few of the use cases that make npx extremely useful:
Run a locally installed package easily
If you wish to execute a locally installed package, all you need to do is type:
npx will check whether
<package> exists in
$PATH, or in the local project binaries and executes it.
Execute packages that are not previously installed
Another major advantage is the ability to execute a package that wasn’t previously installed.
Let’s test this out by running:
This is awesome because sometimes you just want to use some CLI tools but you don’t want to install them globally just to test them out.
This means you can save some disk space and simply run them only when you need them.
This also means your global variables will be less polluted.
Run code directly from GitHub
This one’s pretty rad.
You can use npx to run any GitHub gists and repositories. Let’s focus on executing a GitHub gist because it’s easier to create one.
The most basic script consists of the main JS file and a
After you’ve set up the files, all you have to do is run the npx with the link to that gist as shown in the image above.
Here you can find the code that I used for this example.
Make sure you read carefully any script before you execute it to avoid serious problems that can occur due to malicious code.
Test different package versions
npx makes it extremely easy to test different versions of a Node.js package or module. To test this awesome feature, we’re going to locally install
create-react-app package and test out an upcoming version.
This will list some dist tags near the end of the output. Dist tags provide aliases for version numbers which makes it so much easier to type.
Let’s use npx to try out the
next dist tag of
create-react-app which will create the app inside a sandbox directory.
npx will temporarily install the next version of
create-react-app, and then it’ll execute to scaffold the app and install its dependencies.
Once installed, we can navigate to the app
and then start it with
It will automatically open the React app in your default browser window.
Now we have an app that runs on the next version of
npx helps us avoid versioning, dependency issues, and installing unnecessary packages that we just want to try out. It also provides a clear and easy way of executing packages, commands, modules, and even GitHub gists and repositories.
If you haven’t used npx before, now is a good time to start!
Let me know how would you make use of npx and what do you think about this article through comments below, or on Twitter at @pelu_carol.
If you find this article helpful, please share it with others and subscribe to the blog to support me and receive a bi-monthly-ish e-mail notification on my latest articles.