You can’t be everywhere at once. Neither can node packages. But with a utility called npx — included in npm v5.2 and later — you can interact and run with node modules even if they’re not downloaded locally. The same is true for gists and remote branches of GitHub. As long as npm installed, your work can be wherever you are.
In this course we are going to be talking about a little utility called npx. If you have npm version 5.2 or above, then you already have npx installed on your machine. It enables you to play around with node packages in a way that wasn’t really easy before.
Have you ever wanted to run a locally installed node_module, but you didn’t really want to jump through hoops to do so? Or how about playing around with a package from the npm registry, but you don’t really want to install it globally yet? Well, npx is the tool for those things. You can even play around with experimental GitHub branches with npx, and so much more.
I find myself using npx all the time. It’s one of those tools, where at first you think, "Ohh that’s simple", but then the more you know about it, it can do so much more than you thought at first. So, sit back and enjoy learning about npx.
mkdir npx-test && $_
npm init -y
npm i eslint -D
npm ls eslint
echo "let x = 1; y = 2;" > index.js
Since we previously confirmed that ESLint is installed locally, you might be tempted to start interacting with it on the terminal.
eslint --init
No, that does not work. It doesn't know about my locally installed version. It's looking for something that's globally installed. You could get around this by manually poking into your local Node modules bin folder in order to run ESLint.
./node_modules/.bin/eslint --init
$(npm bin)/eslint --init
npx eslint --init
Wwe’ll temporarily install and invoke a node package from npm that we don’t already have installed globally or locally. This is useful for commands that you don’t use regularly and you’d like to stay up-to-date or for tools you want to play around with temporarily.
create-react-app
npm ls create-react-app --global
npm ls create-react-app
npx create-react-app playground
Also, it could be handy if you just want to play around with some new tools or ideas before committing to them. For example, let's take a look at devpun. We don't have it installed, but we could start using it with npx devpun.
npx devpun -t react
npx devpun -t react | npx cowsay -f vader
However, let's continue to play around. Let's pipe the input of devpun to cowsay, with the Vader Cow file.npx devpun -t react | npx cowsay -f vader | npx lolcatjs
Now, things are getting interesting. Let's continue to play around a bit, and this time, pipe the output to lolcat.js. Wow, colorful.
Using NPX
, we're able to play around and experiment with various Node modules without even installing them globally. Pretty cool.