npx
Introduced in npm version 5.2, the npx binary allows you to run npm package binaries either from the local node_modules/.bin folder, or to run a one-off command without installing package globally. It is a very impressive tool that should replace most npm install global commands.
Installing
If you have npm >= version 5.2 installed you should already have npx - check it is installed using npx -v
If not either upgdate the current version of npm (the latest versions will include npx), or install using
npm install -g npx
Running a local npm binary
When you locally install an npm module the binary file is placed in \node_module\.bin
. You can run these binaries directly from this folder, for instance
.\node_modules\.bin\eslint -v
alternatively you can create a script in your package.json file - any scripts that are ran using npm run <script name>
will have include the .bin folder in the $PATH, so no need to include the full path to the folder.
However, sometimes you will want to run a command without setting up a script or including the full file path, and npx lets you do this. Running
npx eslint -v
will run your locally installed version of eslint, if you want to force it to only run the locally installed file (in case it hasn’t been installed yet, or you have mispelled the exe name), you can use
npx --no-install eslint -v
Running one-off commands
Where npx really shines is one-off commands, where previously you would have installed the package globally you can now just run using npx. This not only reduces the amount of packages you end up installing, but ensures you are always running the latest version (unless you override the package version number obviously). So for commands such as create-react-app, which you run once when starting a project, you can just run it via npx now. Plus you can now easily run incredibly important commands such as:
$ npx cowsay this is silly
_______________
< this is silly >
---------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Globally installed npm packages
You can list all the globally installed packages using
npm ls -g --depth=0
It is worth checking this list to see if you have any packages (such as create-react-app) that you have installed globall that you can remove and use via npx instead.