[translation]This post is from Medium, by Kat Marchan
If you upgrade NPM to the latest version, [email protected], you may find that it installs a new package, NPX.
NPX is a tool designed to improve the experience of using software packages from the NPM registry. NPM makes it very easy to install and manage dependencies hosted on the registry. NPX makes it easy to use CLI tools and other hosted on the registry. It greatly simplifies some things so far, how to install NPX:
Tools installed locally are not availablenpm run-script
Over the past few years, the NPM ecosystem has increasingly localized devDependencies with installation tools as projects, rather than requiring users to install them globally. This means that tools like Mocha, Grunt, and Bower, which were once installed primarily on systems worldwide, can now manage their own versions on a project by project basis. This also means that all you need to do is run the project based on NPM- to make sure you have nodes + NPM on your system, clone the bastard REPo agreement, and do NPM IT to run Install and test. Since NPM run-script adds the local binaries to the path, this works fine!
The downside is that it is not quick/easy to interactively invoke local binaries. There are several ways to do this, and they are all somewhat annoyed with them: You can add these tools scripts, but you need to remember that you can do some shell tricks by using the pass parameter alias NPMX =PATH=$(NPM bin):$PATH, Or you can do them manually with./node_modules/.bin/mocha. These are all jobs, but none of them are very ideal.
NPX gives you what I think is the best solution: $NPX Mocha All you need to do is use a local installation. If you go the extra step and configure shell auto-fallback (more on that below), $Mocha will do the trick for you in the project directory!
In particular, NPX has virtually no overhead if you call an already installed binary – enough to load the tool’s code directly into the currently running Node process! This is as fast as this sort of thing, and makes it a perfectly acceptable scripting tool.
Perform one – off commands
Have you ever wanted to try out some CLI tools, but it was annoying to have to install a global just to run it once? NPX is also great. Call NPX
This feature also works with other package management tools like Yeoman or create-React-app that only install once, and by the time you run them again, they’re already obsolete, so you end up having to run the install every time you want to use them.
As a maintainer of the tool, I like this feature because it means I can put $NPX my-tool into the readme.md directive without the hurdle of trying to get people to actually install it. Frankly, “Oh, just copy and paste this command, it’s zero commitment” is more palatable to users who are unsure whether to use the tool or not.
Here, you might want to try some other interesting packages NPX: happy-Birthday, Benny-Hill, Workin-Hard, Cowsay, Yo, create-React-app, NPM-check. There’s even a full awesome- NPX buyback! Forward! The command to get a full native REST server running is small enough to fit a tweet.
Run different versions of node.js commands
Turns out, there’s this cool package through Ria Stewart calling node on the NPM registry. This means you can use different node versions without having to use a version manager as easily as trying node commands NVM, nave or N. All you need is a stock [email protected] install!
The -pnpx option allows you to specify to install and add to the running package $PATH, which means you can do interesting things like: $NPX -p node@6 NPM it Installs and tests the current NPM package as if node@6 were running globally. I use this myself all the time – even recently had to use it for a project because one of my test libraries was broken node@8. This is a true lifeguard, and I find it easier to use this type of use case than version master, and I always somehow find a way to break or misconfigure.
npm run-script
Interactive development
Many NPM users can now take advantage of the really cool run-script functionality. Not only do they make your $PATH local binaries accessible, they also add a bunch of environment variables that you can access in these scripts. What is you can see these additional variable $NPM run env | grep npm_.
This can make developing and testing scripts to run tricky – meaning that even with tricks like $(NPM bin)/some-bin, you can still not access these magic Env var while working interactively.
But wait! NPX has one more trick: when you use the -c option, the script that writes the string argument will have full access to the same env variable as the regular script that runs it! You can even make a single NPX call using pipes and multiple commands!
Share text-based scripts with friends and loved ones!
Instead of setting up whole Git repos, releasing new tools, etc., it becomes common to share various utility scripts using gist.github.com.
With NPX, you can go one step further: because NPX accepts whatever specifiers NPM itself does, you can create a bullet point that people can call directly with a single command!
Try to use gist.github.com/zkat/4bc195… !
Attention: Stay safe! Be sure to read this carefully when performing these operations, much as you would when running the.sh script!
The shell automatically rolls back
FelixSaparelli adds that this awesome feature means that for many of these use cases, you don’t even need NPX to call directly! The main difference between regular NPX use and fallback is that fallback does not install new packages unless you use the pkg@version syntax: safety net to prevent potentially dangerous sorting.
Setting up automatic fallback is simple: look at the commands for the current shell in the NPX document, add them to.bashrc/.zshrc/.fishrc, and then restart the shell (or refresh the shell using source or some other mechanism).
Now, you can do something like $standard@8 –version to try out different versions of things, and if you’re in an NPM project, $Mocha will automatically go back to the locally installed version of mocha, assuming it’s not installed globally.
DO It!
You can now get NPX by installing [email protected] or updating, or if you don’t want to use NPM, you can install a standalone version of NPX! It is fully compatible with other package managers because any NPM use is only for internal operations. Oh, it’s available in 10 different languages, thanks to the contributions of a large number of early adopters from around the world, –help as well as all system message translations, and can be provided automatically based on system locale! Another example of awesome- NPX repo working well with NPX!