Execute Binaries with npx
If you have a locally installed binary in your project e.g. Webpack, this is how you would execute it :
$ ./node_modules/.bin/webpack
I just “discovered” a new NPM utility called npx
.
If you have npm version > 5.2
installed on your machine, you already have npx installed. Run the following command to verify:
$ which npx
If it’s not available, install it :
$ npm install -g npx
npx
utility provides a shorthand for running binaries installed locally in your project folder.
so instead of this:
~/project_folder
$ ./node_modules/.bin/webpack
you can do this:
~/project_folder
$ npx webpack
One other very handy feature of npx
is the ability to download and execute global binaries without installing them on your local machine. This has the advantage of allowing you to run the most recent version of the binary without the need for an upgrade if outdated.
So, instead of installing http-server
npm package globally, and manually making sure you have the latest version; I can just run the following command in my project root directory and know I’m using the latest version:
~/project (master)
npx http-server
This helps prevent littering your globals with rarely used binaries you installed to play around with.
For a more detailed description, check out the docs