npx vs npm - THE npx ADVANTAGE

npx vs npm - THE npx ADVANTAGE

If you have ever used node, you must have used npm. npm is a dependency/package manager you get out of the box when you install node.

npm is two things really, first; it is an online repository for the publishing of open-source Node.js projects; second, it is a command-line utility for interacting with the said repository that aids in package installation, version management, and dependency management.

Since version npm@5.2.0 - See Release Notes, npx is pre-bundled with npm.

Just like npm, npx is a CLI tool but with special use cases. npx is meant to make it easy to use CLI tools and other executables hosted on the npm registry.

The npx Advantage? (Awesome Use Cases)

Most globally installed packages in node are executables.

You would agree with me that most globally installed node packages are executables. Instead of globally installing node executable packages that you will likely use less often, you can save yourself disk space and simply run them with npx when you need it. This means you can use any node executable package on npm registry without having to install it globally. For example, I don't have to install create-react-app globally on my machine and no worries about updates likewise because anytime I run npx create-react-app, npx will always look up the npm registry and run the create-react-app with its latest version.

Administrative user right

Installing npm packages globally most times requires sudo (administrative user right). With npx you can simply run a node executable anywhere without sudo.

Just want to try out a package

You don't have to install a CLI tool from npm registry when you only want to give it a try.

npx <package/command>

You can also use npx to execute your scripts without adding $PATH variables.

npx ./my-script.js

Interestingly, you can combine npx superpowers with node package executable on npm registry which installs a node binary into your project (so you can have a local version of node that is different than your system's, and manage node like a normal dependency) just like nvm. With this combination, you can run commands with different Node.js versions.

npx node@6 <package/command>

Do you have npx?

Running this should return a path to the npx bin.

$ which npx

If nothing is returned, it means you don't have a version of npm greater than npm@5.2.0 running on your machine. The best bet is to upgrade your npm.

npm install -g npm@latest

Start using npx today