What is NPX?
Matheus Gomes 👨💻
Posted on February 3, 2020
Sup dudes! 🐱🐉
Have you ever installed some global package and needed to use it just a few times? Are you having compatibility problems with the global package because of different versions in multiples projects? Is your machine polluted with many global packages? Worry no more, NPX is here for you.
Today's drops topic is NPX, what is it, what can he do and "why an X, wasn't it NPM?"
First things first 👍
For those who are using the latest version of npm, the npm@5.2.0 there's a new binary, the npx. Before I explain what the npx does, let's talk about binary packages and installations.
First: Global and Local installations
Global installations are made when you need to execute the binary from any directory of your machine.
With Local installation, you can only execute it if you are inside the directory your project is. In that way, it will be added to a script in package.json. If it contains a binary it will be added to node_modules/.bin e
The Problem 👨💻
Almost every time when someone needs to install a package globally is because it will be used in another project. But, sometimes we want to only test it, or in many cases, we install it globally because it's suggested on the documentation. The problem is that in the long term your machine gets "dirty" with all those packages, and always when your project will use another version of the package installed globally it will require to change the version.
Another problem is when your package needs to be used in another project, in that case, you will need to install it manually (in case you don't have an autonomous way to solve this). Not at all convenient as just npm install
.
The solution 😊
NPX. It makes possible to execute a local package as if it's global.
Npx will look for the binary in the node_modules/.bin, if it doesn't find the binary, it will go further down in the files until it finds. If in a case it doesn't find, it will install the binary, execute it and soon after will uninstall it.
So let's say you have a project that you used create-react-app, will be a long time until you run create-react-app again, so why not use npx create-react-app? Or even better, why not use it on your README.md so the people using your project doesn't need to install create-react-app. 0 Commitment!
To use with create-react-app for instance, just type npx create-react-app my-awesome-app
Bonus
Why npx has a 'x'? 🤔
Well, the x mostly likely is for eXecute, but we don't have a clear answer for now.
I hope you guys enjoyed this quick article about npx.
REFERENCES: https://www.youtube.com/watch?v=Sf6JcMz_shU
https://www.npmjs.com/package/npx
https://www.linkedin.com/pulse/npx-uma-alternativa-para-pacotes-globais-e-outras-angelo-medeiros/
https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b
https://stackoverflow.com/questions/50605219/difference-between-npx-and-npm
Posted on February 3, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.