2

**what is the difference between setting up react-native- cli with npm install -g react-native-cli and init versus using npx react-native init ?

I recently got a lot of eneont errors while developing react-native app. firstly i setup my system with npm install -g react-native and was working fine but later while installing dependencies they got eneont errors like could not find fsevent, nanoid, hammerjs etc. And the official documentation includes setup to use npx. I really got confused with this sort of approach. could you please light some knowledge on npm vs npx and installing with '-g' argument?

Mandil Subba
  • 115
  • 1
  • 11

2 Answers2

3

npx is a npm package runner (x probably stands for eXecute). The typical use is to download and run a package temporarily or for trials.

With npm you install the package on your machine. And global makes it available to all your projects not only the one where you currently work in.

yesIamFaded
  • 1,970
  • 2
  • 20
  • 45
  • so how do i manage them so they won't carsh my installation? should i just use npx or only npm? – Mandil Subba Sep 15 '20 at 06:32
  • If you know what to use - and you know what packages your project needs use npm. I always use npm not npx. npm -g I use for react-native installation or expo-cli because you need that in every project. – yesIamFaded Sep 15 '20 at 06:49
  • @MandilSubba did it solve your concerns? If yes consider to accept the answer or ask what is not clear yet – yesIamFaded Sep 15 '20 at 08:17
  • I'm trying your way of handeling the packages but still not satisfied – Mandil Subba Sep 15 '20 at 11:28
  • What do you mean? what does not satisfy you? It is normal react-native programming that you install react-native globally and then navigate to a folder npm react-native init to create a new project. Then you navigate inside this folder and can start programming. When you need a 3rd party library you npm install --save it without the global to just keep it inside the project and not have it installed on your whole machine. – yesIamFaded Sep 15 '20 at 11:34
  • yes the same thing I'm doing right now. i cleaned old files and re installed node as well and am instaling third parties. I'll update you about the result. will that --save break the code? – Mandil Subba Sep 15 '20 at 11:38
  • the same things happens, It work after fresh install but when i attempt to use/ install third party then it show the enoent errors – Mandil Subba Sep 15 '20 at 11:41
  • Well basically you can always omit --save because it happens automatically. But there should not be any permissions error. I guess you are admin on the pc. Maybe start a new project on your C Drive somewhere where you check out the get started from react native and do everything step by step. – yesIamFaded Sep 15 '20 at 12:01
  • maybe try to start the project with npm run start to check if your metro bundler starts and then run it on the emulator in another terminal in VSCode for example – yesIamFaded Sep 15 '20 at 12:03
  • 1
    thank you yeslamFaded, i deleted the file inside npm file inside AppData and now it is working. The -g version and npm install version were different which i guess is the reason that gave enoent as well. I removed them and now the instruction from documentation is working. – Mandil Subba Sep 15 '20 at 14:52
1

As yesIamFaded pointed out, the npx command does indeed download the package for each time you run the code and from a networking standpoint it might beneficial. However, as per React-Native documentation (which you can find here) the cli has been deprecated and it may cause issues. To quote the doc:

If you previously installed a global react-native-cli package, please remove it as it may cause unexpected issues.

So personally i would not go near it.