Skip to main content

Difference between NPM and NPX

npm

npm: node package manager, a tool that allows us to install and manage node packages as dependencies.

  1. Download and install packages locally
npm install typescript

This will create the node_modules directory in your current directory (if one doesn't exist yet) and will download the package to that directory.

npm uninstall lodash

Note:

  • If there is no package.json file in the local directory, the latest version of the package is installed.
  • If there is a package.json file, npm installs the latest version that satisfies the semver rule declared in package.json.
  1. Download and install package globally
npm install -g typescript

Tip: If you are using npm 5.2 or higher, we recommend using npx to run packages globally.

npx

npx: node package runner, makes it really easy to install any sort of node executable that would have normally been installed using npm.

npm install -g npx
  1. Why npx?

Sometimes, instead of using either of the two install methods above, you may just want to use the package and go.

Sometimes, you might just want to experiment with a list of packages as you may not know exactly what you need.

In these cases, ​instead of installing locally or globally, you can go straight to running those packages with NPX.

  1. How does it work?

When you run a package using NPX, it searches for the package in the local (local node_modules/.bin) and global registry (/usr/local/bin), and then it runs the package.

If the package is not already installed, NPX downloads the package files and installs the package, but it will only cache the files instead of saving it.

npx create-react-app my-app --template typescript

One disadvantage of NPX is that it needs to search for packages, whether or not they are installed, before it actually runs them. This, can sometimes be an overhead when you need to get things done very quickly.