The npx Node.js Package Runner
TABLE OF CONTENTS
npx is a very powerful command that's been available in npm starting version 5.2, released in July 2017.
If you don't want to install npm, you can install npx as a standalone package
npx lets you run code built with Node.js and published through the npm registry.
Node.js developers used to publish most of the executable commands as global packages, in order for them to be in the path and executable immediately.
This was a pain because you could not really install different versions of the same command.
npx commandname automatically finds the correct reference of the command inside the
node_modules folder of a project, without needing to know the exact path, and without requiring the package to be installed globally and in the user's path.
There is another great feature of
npx, which is allowing to run commands without first installing them.
This is pretty useful, mostly because:
- you don't need to install anything
- you can run different versions of the same command, using the syntax @version
A typical demonstration of using
npx is through the
cowsay will print a cow saying what you wrote in the command. For example:
cowsay "Hello" will print
_______ < Hello > ------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Now, this if you have the
cowsay command globally installed from npm previously, otherwise you'll get an error when you try to run the command.
npx allows you to run that npm command without having it installed locally:
npx cowsay "Hello"
will do the job.
Now, this is a funny useless command. Other scenarios include:
- running the
vueCLI tool to create new applications and run them:
npx vue create my-vue-app
- creating a new React app using
npx create-react-app my-react-app
and many more.
Once downloaded, the downloaded code will be wiped.
@ to specify the version, and combine that with the
node npm package:
npx node@10 -v #v10.18.1 npx node@12 -v #v12.14.1
This helps to avoid tools like
nvm or the other Node.js version management tools.
npx does not limit you to the packages published on the npm registry.
You can run code that sits in a GitHub gist, for example:
Of course, you need to be careful when running code that you do not control, as with great power comes great responsibility.