npm and npx - What is the difference?

If you’ve ever wondered how npm and npx are different, or what they even are, this post is for you. Learning Nodejs you’ve surely known npm already, while npx is a bit newer. I also wondered about it the first time I saw npx, I even thought someone had made a typo, but it turns out that wasn’t the case. So today I’m going to explain what npm and npx are, and how they differ.

NPM - Node package manager

NPM is the library manager for Nodejs, with the main purpose of automating the management of dependent libraries. For example you’re building a website and you need jquery, you need bootstrap, instead of going online to find and download them or copying a link and pasting it in, npm has got you covered, you just type a few commands and it downloads them and saves them into your project, then all you have to do is call them and use them. Those libraries you need are exactly the “dependents”, the dependencies, also known as packages.

All the dependencies you need will be stored in the package.json file, and to download all of them you just type npm install and you’re done. Of course it also manages the current version of each library, thanks to that when you work together as a team, your project doesn’t break because of version conflicts.

NPX - Node package executor

“Node package executor” is a name I made up myself :))) The homepage only says, briefly, > execute npm package binaries.

Npx is a tool for executing Node packages, shipped together with npm since version 5.2.

Npx works like this, when you type npx some-library-name:

  1. First it looks inside node_modules to see whether this library is already there, and whether it has a valid path to run from. For npx to recognize a library as executable, that library must have a file sitting in the node_modules/.bin directory. To get a file in there, the library itself has to declare the config in its own package.json. (Actually npx will look in $PATH, meaning the operating system’s environment variable, first, but that’s outside the scope of this post so I won’t go deep into it).

For example, specifying the bin setup in the package.json of nodemon, when it’s installed npm will create an executable file in node_modules/.bin:

1
2
3
"bin": {
"nodemon": "./bin/nodemon.js"
}
  1. If it’s there and valid, it just executes the file in the .bin directory.

  2. If the library isn’t there yet, npx will temporarily install it itself and then run it right away (unless you add the --no-install flag, in which case this install step is skipped). For example npx nodemon --no-install. A library installed this way doesn’t sit in the project’s node_modules, it gets saved into npx’s own cache (the _npx directory, for example ~/.npm/_npx on macOS/Linux). It’s called “temporary” but in reality npx doesn’t clear this cache on its own over time, if you want to clean it up you have to delete it by hand.

Why do we need npx and what’s the difference between it and npm?

Without npx, to run a library you’d have to type out the whole path like this node ./node_modules/.bin/example, or declare it in the scripts of package.json.

1
2
3
4
5
6
7
{
"name": "myproject",
"version": "1.0.0",
"scripts": {
"example": "./node_modules/.bin/example"
}
}

Then run it with the command npm run example.

Npx is far simpler, you just type npx example and that’s it, no need to declare any scripts at all.

In summary

That’s the biggest difference between npm and npx, always remember that these two are different and are used for different purposes.

 Comments
Comment plugin failed to load
Loading comment plugin