loading...

What is NPX?

matheusgomes062 profile image Matheus Gomes 👨‍💻 ・3 min read

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

Discussion

pic
Editor guide
Collapse
yvesgurcan profile image
Yves Gurcan

Thanks for sharing! I was excited when npx came out but it turns out that I went a different way: All the packages I need for a project are references in the dependencies in package.json. That way, you don't need to worry about installing something globally at all. I also find it safer to make my projects as self contained as possible. What do you think? Any cons to my method?

Collapse
matheusgomes062 profile image
Matheus Gomes 👨‍💻 Author

I think this is a good way to install the packages, actually is the right way in my opinion. I think npx is more to try packages, and to use with cli, so we don't need to install every time someone wants to see your project.

Collapse
yvesgurcan profile image
Yves Gurcan

Ah, that's right! Once I install the dependency, I write an npm script to run it and if it does not work for me I just remove the dependency and the script. I can see how npx would make it easier for me, as I wouldn't have to use that npm script intermediary when I'm figuring out how the package works. Nice.