What is the difference between npm and npx?

Asked

Viewed 6,959 times

10

I am studying React Activate and noticed that many places use the command:

npx react-native run-android

Why can’t I use the following command?

npm react-native run-android

And what’s the difference between using the npx or direct the package, as in the following example?

react-native run-android
  • npm is for you to manage packages by installing locally, globally or by removing them, npx is for you to run without installing, the command react-native run-android only possible after a global installation, e.g..: nps install -G packageName

  • @Denisrudneidesouza could write a reply with more details, please? (:

2 answers

15


npm is the Node package manager, it will install on your machine a package so that you can use in other projects without having to download again.

ja the npx it will use the package without downloading on your machine, so Voce will install a package on your project or even use this package, without downloading the files on your machine.

For which npx is useful?

  • Already installed some global package and needed to use it very few times?
  • Already had incompatibility problems with global packages by version difference in multiple projects?
  • Have you ever felt that your machine is polluted by several global packages?

When you install React-Native on your machine and start a project using it instead of NPX, you are using the React-Native version of the package installed on your node_modules, whereas when you use npx you use a Node cloud package, then it frees you up to use up other versions that you don’t have on your machine.

  • The npm can only manage front-end packages? o. o

  • 1

    now with Node working in the backend, NPM is a Node package manager, and ends up being a frontend and backend package manager.

  • 1

    Believe me, I get it. (:

2

I will complement and highlight some things in relation to another answer...


What is NPX?

NPX is an NPM package runner that makes it really easy to install any type of node executable that would normally have been installed using NPM. If NPM is a package manager (manager), NPX is a executioner, so the X at the end of the acronym. The X comes from andXecutor.

NPX was developed by Kat Marcán, currently maintained by NPM, which was recently purchased by Github. NPX is a binary that has been present in NPM since version 5.2.

Why use the NPX?

There are several ways to install Node.js packages, you can place them locally (local for the project) or install globally (in the user environment).

Sometimes instead of using one of the above two installation methods, you can just use the package and that’s it.

Sometimes you may just want to try a list of packages as you may not know exactly what you need.

In such cases, instead of installing locally or globally, you can go straight to running these packages with NPX.

How it works?

When you run a package using NPX, it looks for the package on local registry and global and then runs the package.

If the package is not yet installed, NPX downloads the package files and install the package, but only store the cached files instead of saving them, that is, contradicting what was said in the other answer:

already npx it will use the package without having to download on your machine,

To use NPX, you would run a command like this:

npx some-package

A great way to see how fast NPX works is to create a reaction application using:

$ npx create-react-app my-app

The above command will generate a app React, so-called my-app, in the path where the command was executed using the package create-react-app. The NPX then:

  1. Search for the package in your environment locally (node_modules of the project).
  2. If not found, look for the package on your system (node_modules global).
  3. If not found, NPX downloads the files and executes the command to create a new app React, using only that command line.

As shown above, NPX does not only search on the npmjs, as search from within the node_modules of the project also or even in the node_modules before going online to fetch the packages and run.

In the React example, have the react-native cli or create-react-app on the machine takes up space and these libraries are updated very fast, so it doesn’t pay to keep them on the machine. The command create-react-app, for example, it is done once and then never again, unless you start another project.

Observing

One disadvantage of NPX is that it needs to search the packages, whether they are installed or not, before actually running them. All that path I have mentioned (1, 2 and 3) is checked by the NPX script. This can be an overload when you need to do things very quickly, especially if the packages should be searched and/or downloaded to then yes to run.


Source:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.