- Published on
What is npx, the Node Package Runner(x)
- Lorenzo Pieri
Table of Contents
What is npx?
Oh my, Lorenzo, why are you always so late to the party? Because I don't give a single ef, that's why.
I don't care if you've read it a 1000 times in other blogs somewhere else, you were the one to click on the link in the first place, hence I guess you are either here to learn something, to freshen up your memory or to cry about me not writing new web content.
No f$!ks were given that day, trust me.
npx stands for Node Package Runner. Yes, the
x stands for
Runner because it is usually associated with
execution, and it totally makes sense.
npx does one job and it does that pretty f**kin well. It executes NodeJs binaries at runtime so that you don't have to install anything if you just want to give it a try or the execution on its own is enough for your project to complete.
Why?, you ask. Well, to put it simple, have you ever needed to run a command once or even once in a while, without installing libraries, binaries or dependencies and just forget about that?
Well, that's what
npx is for. Think
npx create-next-app@latest. If you manage to use the Node Package Runner instead of the Node Package Manager (
npm) you can keep your computer pretty clean and you can always choose which binaries you want to use for your projects.
Maybe today you feel like React@16 is your best friend, maybe tomorrow you will want to hate on the world using React@18. Why stop yourself from being able to venture forth in this wonderful world of technologies! Just type
How to use npx
As you might have guessed from the rant a little above the fold, the usage of
npx is pretty simple. You just write the command (after you've installed it, although now it comes preinstalled with newer NodeJs versions) and the package you want to run. That's it.
npx create-react-app <name of your app> # ... App is being created
At the end of the day, the biggest difference is the one that we already stated.
npx runs the binaries directly while
npm installs the binaries for future local usage inside your systems. Those binaries can be installed both locally or globally but they will remain on your computers.
Sometimes that's an optimal choice, other times, as long as we have an Internet connection pretty much, we can assume that
npx could be the best choice to remain up-to-date with our packages because we don't really install them.
Am I saying that you won't do
npm install --save-dev <dependency> anymore? Of course not. We are talking about execution of binaries at call time, not your regular project scaffolding.
npx better than
So, no, it's not better, it's just different and it has different usages so that you can make the best out of both, since both come preinstalled on your NodeJs installations.
Having fun with
There are a lot of fun and useful commands that you can run on your CLI. Here's a few that you can use to have fun with!
npx json-server <name of file>, allows you to start a RESTful CRUD server based on a json file configuration.
npx server, starts a local server on the folder you are on when you run the command.
npx readme-md-generator, invokes a readme builder to make a fancy github profile.
npx snyk test, a security testing utility for your programs.
npx cowsay <phrase>, runs the
cowsayCLI command with a phrase.
You can also run packages from URLs, like on Gist, as long as the executable is defined #!/usr/bin/node
npx https://<your-nodejs-program/, like so.
npx npackr, a CLI utility to read package.json commands on the fly (this is a package I've done myself!).
I hope you found this article useful and to your liking and if you have any requests, drop a message on one of my social media accounts or open an issue/start a discussion on github, on this repository!
As always you can find me on Twitter, listen to my Podcast on Spotify and add me on LinkedIn to talk professionally (yeah, right)